|
| 1 | +import fs from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | +import {fileURLToPath} from 'node:url'; |
| 4 | +import test from 'ava'; |
| 5 | +import createProviderMacro from './_with-provider.js'; |
| 6 | + |
| 7 | +const projectDir = path.dirname(fileURLToPath(import.meta.url)); |
| 8 | +const pkg = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url))); |
| 9 | +const withProvider = createProviderMacro('ava-6', '5.3.0'); |
| 10 | + |
| 11 | +const validateConfig = (t, provider, config) => { |
| 12 | + const error = t.throws(() => provider.main({config})); |
| 13 | + error.message = error.message.replace(`v${pkg.version}`, 'v${pkg.version}'); // eslint-disable-line no-template-curly-in-string |
| 14 | + t.snapshot(error); |
| 15 | +}; |
| 16 | + |
| 17 | +test('negotiates ava-6 protocol', withProvider, t => t.plan(2)); |
| 18 | + |
| 19 | +test('main() config validation: throw when config is not a plain object', withProvider, (t, provider) => { |
| 20 | + validateConfig(t, provider, false); |
| 21 | + validateConfig(t, provider, true); |
| 22 | + validateConfig(t, provider, null); |
| 23 | + validateConfig(t, provider, []); |
| 24 | +}); |
| 25 | + |
| 26 | +test('main() config validation: throw when config contains keys other than \'extensions\', \'rewritePaths\' or \'compile\'', withProvider, (t, provider) => { |
| 27 | + validateConfig(t, provider, {compile: false, foo: 1, rewritePaths: {'src/': 'build/'}}); |
| 28 | +}); |
| 29 | + |
| 30 | +test('main() config validation: throw when config.extensions contains empty strings', withProvider, (t, provider) => { |
| 31 | + validateConfig(t, provider, {extensions: ['']}); |
| 32 | +}); |
| 33 | + |
| 34 | +test('main() config validation: throw when config.extensions contains non-strings', withProvider, (t, provider) => { |
| 35 | + validateConfig(t, provider, {extensions: [1]}); |
| 36 | +}); |
| 37 | + |
| 38 | +test('main() config validation: throw when config.extensions contains duplicates', withProvider, (t, provider) => { |
| 39 | + validateConfig(t, provider, {extensions: ['ts', 'ts']}); |
| 40 | +}); |
| 41 | + |
| 42 | +test('main() config validation: config may not be an empty object', withProvider, (t, provider) => { |
| 43 | + validateConfig(t, provider, {}); |
| 44 | +}); |
| 45 | + |
| 46 | +test('main() config validation: throw when config.compile is invalid', withProvider, (t, provider) => { |
| 47 | + validateConfig(t, provider, {rewritePaths: {'src/': 'build/'}, compile: 1}); |
| 48 | + validateConfig(t, provider, {rewritePaths: {'src/': 'build/'}, compile: undefined}); |
| 49 | +}); |
| 50 | + |
| 51 | +test('main() config validation: rewrite paths must end in a /', withProvider, (t, provider) => { |
| 52 | + validateConfig(t, provider, {rewritePaths: {src: 'build/', compile: false}}); |
| 53 | + validateConfig(t, provider, {rewritePaths: {'src/': 'build', compile: false}}); |
| 54 | +}); |
| 55 | + |
| 56 | +test('main() extensions: defaults to [\'ts\', \'cts\', \'mts\']', withProvider, (t, provider) => { |
| 57 | + t.deepEqual(provider.main({config: {rewritePaths: {'src/': 'build/'}, compile: false}}).extensions, ['ts', 'cts', 'mts']); |
| 58 | +}); |
| 59 | + |
| 60 | +test('main() extensions: returns configured extensions', withProvider, (t, provider) => { |
| 61 | + const extensions = ['tsx']; |
| 62 | + t.deepEqual(provider.main({config: {extensions, rewritePaths: {'src/': 'build/'}, compile: false}}).extensions, extensions); |
| 63 | +}); |
| 64 | + |
| 65 | +test('main() extensions: always returns new arrays', withProvider, (t, provider) => { |
| 66 | + const main = provider.main({config: {rewritePaths: {'src/': 'build/'}, compile: false}}); |
| 67 | + t.not(main.extensions, main.extensions); |
| 68 | +}); |
| 69 | + |
| 70 | +test('main() updateGlobs()', withProvider, (t, provider) => { |
| 71 | + const main = provider.main({config: {rewritePaths: {'src/': 'build/'}, compile: false}}); |
| 72 | + t.snapshot(main.updateGlobs({ |
| 73 | + filePatterns: ['src/test.ts'], |
| 74 | + ignoredByWatcherPatterns: ['assets/**'], |
| 75 | + })); |
| 76 | +}); |
| 77 | + |
| 78 | +test('main() interpretChange() without compilation', withProvider, (t, provider) => { |
| 79 | + const main = provider.main({config: {rewritePaths: {'src/': 'build/'}, compile: false}}); |
| 80 | + t.is(main.interpretChange(path.join(projectDir, 'src/foo.ts')), main.changeInterpretations.waitForOutOfBandCompilation); |
| 81 | + t.is(main.interpretChange(path.join(projectDir, 'build/foo.js')), main.changeInterpretations.unspecified); |
| 82 | + t.is(main.interpretChange(path.join(projectDir, 'src/foo.txt')), main.changeInterpretations.unspecified); |
| 83 | +}); |
| 84 | + |
| 85 | +test('main() interpretChange() with compilation', withProvider, (t, provider) => { |
| 86 | + const main = provider.main({config: {rewritePaths: {'src/': 'build/'}, compile: 'tsc'}}); |
| 87 | + t.is(main.interpretChange(path.join(projectDir, 'src/foo.ts')), main.changeInterpretations.unspecified); |
| 88 | + t.is(main.interpretChange(path.join(projectDir, 'build/foo.js')), main.changeInterpretations.ignoreCompiled); |
| 89 | + t.is(main.interpretChange(path.join(projectDir, 'src/foo.txt')), main.changeInterpretations.unspecified); |
| 90 | +}); |
| 91 | + |
| 92 | +test('main() resolvePossibleOutOfBandCompilationSources() with compilation', withProvider, (t, provider) => { |
| 93 | + const main = provider.main({config: {rewritePaths: {'src/': 'build/'}, compile: 'tsc'}}); |
| 94 | + t.is(main.resolvePossibleOutOfBandCompilationSources(path.join(projectDir, 'build/foo.js')), null); |
| 95 | +}); |
| 96 | + |
| 97 | +test('main() resolvePossibleOutOfBandCompilationSources() unknown extension', withProvider, (t, provider) => { |
| 98 | + const main = provider.main({config: {rewritePaths: {'src/': 'build/'}, compile: false}}); |
| 99 | + t.is(main.resolvePossibleOutOfBandCompilationSources(path.join(projectDir, 'build/foo.bar')), null); |
| 100 | +}); |
| 101 | + |
| 102 | +test('main() resolvePossibleOutOfBandCompilationSources() not a build path', withProvider, (t, provider) => { |
| 103 | + const main = provider.main({config: {rewritePaths: {'src/': 'build/'}, compile: false}}); |
| 104 | + t.is(main.resolvePossibleOutOfBandCompilationSources(path.join(projectDir, 'lib/foo.js')), null); |
| 105 | +}); |
| 106 | + |
| 107 | +test('main() resolvePossibleOutOfBandCompilationSources() .cjs but .cts not configured', withProvider, (t, provider) => { |
| 108 | + const main = provider.main({config: {extensions: ['ts'], rewritePaths: {'src/': 'build/'}, compile: false}}); |
| 109 | + t.is(main.resolvePossibleOutOfBandCompilationSources(path.join(projectDir, 'build/foo.cjs')), null); |
| 110 | +}); |
| 111 | + |
| 112 | +test('main() resolvePossibleOutOfBandCompilationSources() .mjs but .mts not configured', withProvider, (t, provider) => { |
| 113 | + const main = provider.main({config: {extensions: ['ts'], rewritePaths: {'src/': 'build/'}, compile: false}}); |
| 114 | + t.is(main.resolvePossibleOutOfBandCompilationSources(path.join(projectDir, 'build/foo.mjs')), null); |
| 115 | +}); |
| 116 | + |
| 117 | +test('main() resolvePossibleOutOfBandCompilationSources() .js but .ts not configured', withProvider, (t, provider) => { |
| 118 | + const main = provider.main({config: {extensions: ['cts'], rewritePaths: {'src/': 'build/'}, compile: false}}); |
| 119 | + t.is(main.resolvePossibleOutOfBandCompilationSources(path.join(projectDir, 'build/foo.js')), null); |
| 120 | +}); |
| 121 | + |
| 122 | +test('main() resolvePossibleOutOfBandCompilationSources() .cjs and .cjs and .cts configured', withProvider, (t, provider) => { |
| 123 | + const main = provider.main({config: {extensions: ['cjs', 'cts'], rewritePaths: {'src/': 'build/'}, compile: false}}); |
| 124 | + t.deepEqual(main.resolvePossibleOutOfBandCompilationSources(path.join(projectDir, 'build/foo.cjs')), [path.join(projectDir, 'src/foo.cjs'), path.join(projectDir, 'src/foo.cts')]); |
| 125 | +}); |
| 126 | + |
| 127 | +test('main() resolvePossibleOutOfBandCompilationSources() .mjs and .mjs and .mts configured', withProvider, (t, provider) => { |
| 128 | + const main = provider.main({config: {extensions: ['mjs', 'mts'], rewritePaths: {'src/': 'build/'}, compile: false}}); |
| 129 | + t.deepEqual(main.resolvePossibleOutOfBandCompilationSources(path.join(projectDir, 'build/foo.mjs')), [path.join(projectDir, 'src/foo.mjs'), path.join(projectDir, 'src/foo.mts')]); |
| 130 | +}); |
| 131 | + |
| 132 | +test('main() resolvePossibleOutOfBandCompilationSources() .js and .js, .ts and .tsx configured', withProvider, (t, provider) => { |
| 133 | + const main = provider.main({config: {extensions: ['js', 'ts', 'tsx'], rewritePaths: {'src/': 'build/'}, compile: false}}); |
| 134 | + t.deepEqual(main.resolvePossibleOutOfBandCompilationSources(path.join(projectDir, 'build/foo.js')), [path.join(projectDir, 'src/foo.js'), path.join(projectDir, 'src/foo.ts'), path.join(projectDir, 'src/foo.tsx')]); |
| 135 | +}); |
| 136 | + |
| 137 | +test('main() resolvePossibleOutOfBandCompilationSources() returns the first possible path that exists', withProvider, (t, provider) => { |
| 138 | + const main = provider.main({config: {extensions: ['js', 'ts', 'tsx'], rewritePaths: {'fixtures/load/': 'fixtures/load/compiled/'}, compile: false}}); |
| 139 | + t.deepEqual(main.resolvePossibleOutOfBandCompilationSources(path.join(projectDir, 'fixtures/load/compiled/index.js')), [path.join(projectDir, 'fixtures/load/index.ts')]); |
| 140 | +}); |
0 commit comments