Skip to content

Commit

Permalink
test(browser): add test case for cjs import (vitest-dev#4690)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Dec 9, 2023
1 parent a7931bb commit 285bc61
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 2 deletions.
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions test/browser/cjs-lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
a: 'a',
b: 'b',
object: {
h: 'h',
},
}
7 changes: 7 additions & 0 deletions test/browser/cjs-lib/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@vitest/cjs-lib",
"type": "commonjs",
"exports": {
"default": "./index.js"
}
}
1 change: 1 addition & 0 deletions test/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"devDependencies": {
"@vitest/browser": "workspace:*",
"@vitest/cjs-lib": "link:./cjs-lib",
"execa": "^7.1.1",
"safaridriver": "^0.0.4",
"vitest": "workspace:*"
Expand Down
4 changes: 2 additions & 2 deletions test/browser/specs/runner.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ const passedTests = getPassed(browserResultJson.testResults)
const failedTests = getFailed(browserResultJson.testResults)

await test('tests are actually running', async () => {
assert.ok(browserResultJson.testResults.length === 8, 'Not all the tests have been run')
assert.ok(passedTests.length === 7, 'Some tests failed')
assert.ok(browserResultJson.testResults.length === 9, 'Not all the tests have been run')
assert.ok(passedTests.length === 8, 'Some tests failed')
assert.ok(failedTests.length === 1, 'Some tests have passed but should fail')

assert.doesNotMatch(stderr, /Unhandled Error/, 'doesn\'t have any unhandled errors')
Expand Down
21 changes: 21 additions & 0 deletions test/browser/test/cjs-lib.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { expect, test } from 'vitest'
import cjsDefault, { a as cjsNamed } from '@vitest/cjs-lib'
import * as cjsNamespace from '@vitest/cjs-lib'

test('cjs namespace import', () => {
expect(cjsNamespace).toEqual({
a: 'a',
b: 'b',
object: {
h: 'h',
},
})
})

test('cjs named import', () => {
expect(cjsNamed).toEqual('a')
})

test('cjs default import not supported when slowHijackESM', () => {
expect(cjsDefault).toBeUndefined()
})
3 changes: 3 additions & 0 deletions test/browser/vitest.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const dir = dirname(fileURLToPath(import.meta.url))
function noop() {}

export default defineConfig({
optimizeDeps: {
include: ['@vitest/cjs-lib'],
},
test: {
include: ['test/**.test.{ts,js}'],
browser: {
Expand Down

0 comments on commit 285bc61

Please sign in to comment.