Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add jest example/test #224

Merged
merged 10 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/bug-report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ body:

Please use a template below to create a minimal reproduction
👉 https://stackblitz.com/github/nuxt/test-utils/tree/main/examples/app
👉 https://stackblitz.com/github/nuxt/test-utils/tree/main/examples/app-jest
👉 https://stackblitz.com/github/nuxt/test-utils/tree/main/examples/module
- type: textarea
id: bug-env
Expand Down
1 change: 1 addition & 0 deletions .github/reproduire/needs-reproduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ If `needs reproduction` labeled issues don't receive any substantial activity (e
We have a couple of templates for starting with a minimal reproduction:

👉 https://stackblitz.com/github/nuxt/test-utils/tree/main/examples/app
👉 https://stackblitz.com/github/nuxt/test-utils/tree/main/examples/app-jest
👉 https://stackblitz.com/github/nuxt/test-utils/tree/main/examples/module

A public GitHub repository is also perfect. 👌
Expand Down
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
shell-emulator=true
3 changes: 3 additions & 0 deletions examples/app-jest/.stackblitz/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"startCommand": "npm run test"
}
5 changes: 5 additions & 0 deletions examples/app-jest/app.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<div>
<NuxtWelcome />
</div>
</template>
17 changes: 17 additions & 0 deletions examples/app-jest/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

import type { Config } from 'jest'

export default {
preset: 'ts-jest/presets/default-esm', // or other ESM presets
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
transform: {
'^.+\\.tsx?$': [
'ts-jest',
{
useESM: true,
},
],
},
} satisfies Config
7 changes: 7 additions & 0 deletions examples/app-jest/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

import { defineNuxtConfig } from 'nuxt/config'

// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
devtools: { enabled: true }
})
27 changes: 27 additions & 0 deletions examples/app-jest/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "nuxt-app",
"private": true,
"type": "module",
"scripts": {
"build": "nuxt build",
"dev": "nuxt dev",
"generate": "nuxt generate",
"preview": "nuxt preview",
"postinstall": "nuxt prepare",
"test": "node --no-warnings --experimental-vm-modules node_modules/jest/bin/jest.js"
},
"dependencies": {
"nuxt": "^3.8.0",
"vue": "^3.3.7",
"vue-router": "^4.2.5"
},
"devDependencies": {
"@nuxt/test-utils": "latest",
"@types/jest": "^29.5.7",
"jest": "^29.7.0",
"playwright-core": "1.39.0",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
}
}
3 changes: 3 additions & 0 deletions examples/app-jest/server/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../.nuxt/tsconfig.server.json"
}
18 changes: 18 additions & 0 deletions examples/app-jest/test/browser.e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { fileURLToPath } from 'node:url'
import { createPage, setup } from '@nuxt/test-utils'

await setup({
rootDir: fileURLToPath(new URL('../', import.meta.url)),
browser: true,
runner: 'jest',
})

describe('browser', () => {
it('runs a test', async () => {
const page = await createPage('/')
const text = await page.getByRole('heading',{ name: 'Welcome to Nuxt!' }).innerText()
expect(text).toContain(
'Welcome to Nuxt!'
)
})
})
15 changes: 15 additions & 0 deletions examples/app-jest/test/dev.e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { fileURLToPath } from 'node:url'
import { $fetch, setup } from '@nuxt/test-utils'

await setup({
rootDir: fileURLToPath(new URL('../', import.meta.url)),
dev: true,
runner: 'jest',
})

describe('server (dev)', () => {
it('runs a test', async () => {
const html = await $fetch('/')
expect(html.slice(0, 15)).toMatchInlineSnapshot(`"<!DOCTYPE html>"`)
})
})
14 changes: 14 additions & 0 deletions examples/app-jest/test/server.e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { fileURLToPath } from 'node:url'
import { $fetch, setup } from '@nuxt/test-utils'

await setup({
rootDir: fileURLToPath(new URL('../', import.meta.url)),
runner: 'jest'
})

describe("app",()=>{
it("runs a test",async ()=>{
const html = await $fetch('/')
expect(html.slice(0, 15)).toMatchInlineSnapshot(`"<!DOCTYPE html>"`)
})
})
14 changes: 14 additions & 0 deletions examples/app-jest/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "./.nuxt/tsconfig.json",
"compilerOptions": {
"module": "ESNext",
"esModuleInterop": true,
"moduleResolution": "Bundler",
"verbatimModuleSyntax": false,
"target": "ESNext",
"types": [
"jest"
],
"resolveJsonModule": true
}
}
Loading
Loading