|
| 1 | +import path from 'node:path' |
1 | 2 | import { expect, test } from 'vitest' |
2 | | -import { runInlineTests } from '../../test-utils' |
| 3 | +import { rolldownVersion } from 'vitest/node' |
| 4 | +import { runInlineTests, runVitest } from '../../test-utils' |
3 | 5 |
|
4 | 6 | test('setting resetMocks works if restoreMocks is also set', async () => { |
5 | 7 | const { stderr, testTree } = await runInlineTests({ |
@@ -39,3 +41,115 @@ test('spy is not called here', () => { |
39 | 41 | } |
40 | 42 | `) |
41 | 43 | }) |
| 44 | + |
| 45 | +test('invalid packages', async () => { |
| 46 | + const { results, errorTree } = await runVitest({ |
| 47 | + root: path.join(import.meta.dirname, '../fixtures/invalid-package'), |
| 48 | + }) |
| 49 | + const testModuleErrors = Object.fromEntries( |
| 50 | + results.map(testModule => [ |
| 51 | + testModule.relativeModuleId, |
| 52 | + testModule.errors().map(e => e.message), |
| 53 | + ]), |
| 54 | + ) |
| 55 | + |
| 56 | + // requires Vite 8 for relaxed import analysis validataion |
| 57 | + // https://github.com/vitejs/vite/pull/21601 |
| 58 | + if (rolldownVersion) { |
| 59 | + expect(testModuleErrors).toMatchInlineSnapshot(` |
| 60 | + { |
| 61 | + "mock-bad-dep.test.ts": [], |
| 62 | + "mock-wrapper-and-bad-dep.test.ts": [], |
| 63 | + "mock-wrapper.test.ts": [], |
| 64 | + } |
| 65 | + `) |
| 66 | + expect(errorTree()).toMatchInlineSnapshot(` |
| 67 | + { |
| 68 | + "mock-bad-dep.test.ts": { |
| 69 | + "basic": "passed", |
| 70 | + }, |
| 71 | + "mock-wrapper-and-bad-dep.test.ts": { |
| 72 | + "basic": "passed", |
| 73 | + }, |
| 74 | + "mock-wrapper.test.ts": { |
| 75 | + "basic": "passed", |
| 76 | + }, |
| 77 | + } |
| 78 | + `) |
| 79 | + } |
| 80 | + else { |
| 81 | + expect(testModuleErrors).toMatchInlineSnapshot(` |
| 82 | + { |
| 83 | + "mock-bad-dep.test.ts": [ |
| 84 | + "Failed to resolve entry for package "test-dep-invalid". The package may have incorrect main/module/exports specified in its package.json.", |
| 85 | + ], |
| 86 | + "mock-wrapper-and-bad-dep.test.ts": [ |
| 87 | + "Failed to resolve entry for package "test-dep-invalid". The package may have incorrect main/module/exports specified in its package.json.", |
| 88 | + ], |
| 89 | + "mock-wrapper.test.ts": [ |
| 90 | + "Failed to resolve entry for package "test-dep-invalid". The package may have incorrect main/module/exports specified in its package.json.", |
| 91 | + ], |
| 92 | + } |
| 93 | + `) |
| 94 | + expect(errorTree()).toMatchInlineSnapshot(` |
| 95 | + { |
| 96 | + "mock-bad-dep.test.ts": {}, |
| 97 | + "mock-wrapper-and-bad-dep.test.ts": {}, |
| 98 | + "mock-wrapper.test.ts": {}, |
| 99 | + } |
| 100 | + `) |
| 101 | + } |
| 102 | +}) |
| 103 | + |
| 104 | +test('mocking modules with syntax error', async () => { |
| 105 | + // TODO: manual mocked module still gets transformed so this is not supported yet. |
| 106 | + const { errorTree, results } = await runInlineTests({ |
| 107 | + './syntax-error.js': `syntax error`, |
| 108 | + './basic.test.js': /* ts */ ` |
| 109 | +import * as dep from './syntax-error.js' |
| 110 | +
|
| 111 | +vi.mock('./syntax-error.js', () => { |
| 112 | + return { mocked: 'ok' } |
| 113 | +}) |
| 114 | +
|
| 115 | +test('can mock invalid module', () => { |
| 116 | + expect(dep).toMatchObject({ mocked: 'ok' }) |
| 117 | +}) |
| 118 | + `, |
| 119 | + }) |
| 120 | + |
| 121 | + const testModuleErrors = Object.fromEntries( |
| 122 | + results.map(testModule => [ |
| 123 | + testModule.relativeModuleId, |
| 124 | + testModule.errors().map(e => e.message), |
| 125 | + ]), |
| 126 | + ) |
| 127 | + if (rolldownVersion) { |
| 128 | + expect(testModuleErrors).toMatchInlineSnapshot(` |
| 129 | + { |
| 130 | + "basic.test.js": [ |
| 131 | + "Parse failure: Parse failed with 1 error: |
| 132 | + Expected a semicolon or an implicit semicolon after a statement, but found none |
| 133 | + 1: syntax error |
| 134 | + ^ |
| 135 | + At file: /syntax-error.js:1:6", |
| 136 | + ], |
| 137 | + } |
| 138 | + `) |
| 139 | + } |
| 140 | + else { |
| 141 | + expect(testModuleErrors).toMatchInlineSnapshot(` |
| 142 | + { |
| 143 | + "basic.test.js": [ |
| 144 | + "Parse failure: Expected ';', '}' or <eof> |
| 145 | + At file: /syntax-error.js:1:7", |
| 146 | + ], |
| 147 | + } |
| 148 | + `) |
| 149 | + } |
| 150 | + expect(errorTree()).toMatchInlineSnapshot(` |
| 151 | + { |
| 152 | + "basic.test.js": {}, |
| 153 | + } |
| 154 | + `) |
| 155 | +}) |
0 commit comments