From d6a5f05f69cdde8a45fbd4c341f26bad8b1127c9 Mon Sep 17 00:00:00 2001 From: Hiroki Osame Date: Sat, 11 May 2024 00:45:14 +0900 Subject: [PATCH] test: upgrade fs-fixture --- package.json | 2 +- pnpm-lock.yaml | 12 ++--- tests/specs/api.ts | 110 +++++++++++++++++++++---------------------- tests/specs/cli.ts | 30 +++++------- tests/specs/smoke.ts | 27 ++++------- 5 files changed, 84 insertions(+), 97 deletions(-) diff --git a/package.json b/package.json index 78fc5a743..f12e7105b 100644 --- a/package.json +++ b/package.json @@ -86,7 +86,7 @@ "cross-spawn": "^7.0.3", "es-module-lexer": "^1.5.2", "execa": "^8.0.1", - "fs-fixture": "^1.2.0", + "fs-fixture": "^2.4.0", "fs-require": "^1.6.0", "get-node": "^15.0.0", "kolorist": "^1.8.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 85b339898..aa0051252 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -53,8 +53,8 @@ importers: specifier: ^8.0.1 version: 8.0.1 fs-fixture: - specifier: ^1.2.0 - version: 1.2.0 + specifier: ^2.4.0 + version: 2.4.0 fs-require: specifier: ^1.6.0 version: 1.6.0 @@ -1710,9 +1710,9 @@ packages: fraction.js@4.3.7: resolution: {integrity: sha512-ZsDfxO51wGAXREY55a7la9LScWpwv9RxIrYABrlvOFBlH/ShPnrtsXeuUIfXKKOVicNxQ+o8JTbJvjS4M89yew==} - fs-fixture@1.2.0: - resolution: {integrity: sha512-bPBNW12US81zxCXzP/BQ6ntSvMXNgX76nHVUxzQJVTmHkzXnbfp+M4mNWeJ9LuCG8M1wyeFov7oiwO9wnCEBjQ==} - engines: {node: '>=16.7.0'} + fs-fixture@2.4.0: + resolution: {integrity: sha512-aFoTWGj288IEOdXBeesdcbdMvRtExbqpOp1SCjE3nRdlT7vBBCD6bf76C9FCq8/6pIPSo56P7+HeT9zT/n8rMA==} + engines: {node: '>=18.0.0'} fs-require@1.6.0: resolution: {integrity: sha512-zk5lFDV09Ef+CwOuPI/Q46lNdyzypPH/dB4Ywnyt40l2yqe5Z8ZJBGFaaYHwOwXAtKJEW4lSsn6V26erlEXVGA==} @@ -5079,7 +5079,7 @@ snapshots: fraction.js@4.3.7: {} - fs-fixture@1.2.0: {} + fs-fixture@2.4.0: {} fs-require@1.6.0: {} diff --git a/tests/specs/api.ts b/tests/specs/api.ts index 0e330764c..456672a32 100644 --- a/tests/specs/api.ts +++ b/tests/specs/api.ts @@ -1,4 +1,3 @@ -import path from 'node:path'; import { execaNode } from 'execa'; import { testSuite, expect } from 'manten'; import { createFixture } from 'fs-fixture'; @@ -34,14 +33,13 @@ const tsFiles = { export default testSuite(({ describe }, node: NodeApis) => { describe('API', ({ describe }) => { describe('CommonJS', ({ test }) => { - test('cli', async ({ onTestFinish }) => { - const fixture = await createFixture({ + test('cli', async () => { + await using fixture = await createFixture({ 'index.ts': 'import { message } from \'./file\';\n\nconsole.log(message, new Error().stack);', ...tsFiles, }); - onTestFinish(async () => await fixture.rm()); - const { stdout } = await execaNode(path.join(fixture.path, 'index.ts'), { + const { stdout } = await execaNode(fixture.getPath('index.ts'), { nodePath: node.path, nodeOptions: ['--require', tsxCjsPath], }); @@ -49,8 +47,8 @@ export default testSuite(({ describe }, node: NodeApis) => { expect(stdout).toContain('index.ts:3:22'); }); - test('register / unregister', async ({ onTestFinish }) => { - const fixture = await createFixture({ + test('register / unregister', async () => { + await using fixture = await createFixture({ 'register.cjs': ` const { register } = require(${JSON.stringify(tsxCjsApiPath)}); try { @@ -78,9 +76,8 @@ export default testSuite(({ describe }, node: NodeApis) => { `, ...tsFiles, }); - onTestFinish(async () => await fixture.rm()); - const { stdout } = await execaNode(path.join(fixture.path, 'register.cjs'), [], { + const { stdout } = await execaNode(fixture.getPath('register.cjs'), [], { nodePath: node.path, nodeOptions: [], }); @@ -89,8 +86,8 @@ export default testSuite(({ describe }, node: NodeApis) => { }); describe('tsx.require()', ({ test }) => { - test('loads', async ({ onTestFinish }) => { - const fixture = await createFixture({ + test('loads', async () => { + await using fixture = await createFixture({ 'require.cjs': ` const path = require('node:path'); const tsx = require(${JSON.stringify(tsxCjsApiPath)}); @@ -116,9 +113,8 @@ export default testSuite(({ describe }, node: NodeApis) => { `, ...tsFiles, }); - onTestFinish(async () => await fixture.rm()); - const { stdout } = await execaNode(path.join(fixture.path, 'require.cjs'), [], { + const { stdout } = await execaNode(fixture.getPath('require.cjs'), [], { nodePath: node.path, nodeOptions: [], }); @@ -126,17 +122,16 @@ export default testSuite(({ describe }, node: NodeApis) => { expect(stdout).toBe('Fails as expected\nfoo bar\nfile.ts\nUnpolluted global require'); }); - test('catchable', async ({ onTestFinish }) => { - const fixture = await createFixture({ + test('catchable', async () => { + await using fixture = await createFixture({ 'require.cjs': ` const tsx = require(${JSON.stringify(tsxCjsApiPath)}); try { tsx.require('./file', __filename); } catch {} `, 'file.ts': 'if', }); - onTestFinish(async () => await fixture.rm()); - const { all } = await execaNode(path.join(fixture.path, 'require.cjs'), [], { + const { all } = await execaNode(fixture.getPath('require.cjs'), [], { nodePath: node.path, nodeOptions: [], all: true, @@ -147,15 +142,14 @@ export default testSuite(({ describe }, node: NodeApis) => { }); describe('module', ({ describe, test }) => { - test('cli', async ({ onTestFinish }) => { - const fixture = await createFixture({ + test('cli', async () => { + await using fixture = await createFixture({ 'package.json': JSON.stringify({ type: 'module' }), 'index.ts': 'import { message } from \'./file\';\n\nconsole.log(message, new Error().stack);', ...tsFiles, }); - onTestFinish(async () => await fixture.rm()); - const { stdout } = await execaNode(path.join(fixture.path, 'index.ts'), { + const { stdout } = await execaNode(fixture.getPath('index.ts'), { nodePath: node.path, nodeOptions: [node.supports.moduleRegister ? '--import' : '--loader', tsxEsmPath], }); @@ -164,8 +158,8 @@ export default testSuite(({ describe }, node: NodeApis) => { }); if (node.supports.moduleRegister) { - test('module.register', async ({ onTestFinish }) => { - const fixture = await createFixture({ + test('module.register', async () => { + await using fixture = await createFixture({ 'package.json': JSON.stringify({ type: 'module' }), 'module-register.mjs': ` import { register } from 'node:module'; @@ -184,9 +178,8 @@ export default testSuite(({ describe }, node: NodeApis) => { `, ...tsFiles, }); - onTestFinish(async () => await fixture.rm()); - const { stdout } = await execaNode(path.join(fixture.path, 'module-register.mjs'), [], { + const { stdout } = await execaNode(fixture.getPath('module-register.mjs'), [], { nodePath: node.path, nodeOptions: [], }); @@ -195,8 +188,8 @@ export default testSuite(({ describe }, node: NodeApis) => { }); describe('register / unregister', ({ test }) => { - test('register / unregister', async ({ onTestFinish }) => { - const fixture = await createFixture({ + test('register / unregister', async () => { + await using fixture = await createFixture({ 'package.json': JSON.stringify({ type: 'module' }), 'register.mjs': ` import { register } from ${JSON.stringify(tsxEsmApiPath)}; @@ -206,32 +199,42 @@ export default testSuite(({ describe }, node: NodeApis) => { console.log('Fails as expected 1'); } - const unregister = register(); - - const { message } = await import('./file?2'); - console.log(message); - - await unregister(); + { + const unregister = register(); + const { message } = await import('./file?2'); + console.log(message); + + await unregister(); + } + try { await import('./file.ts?3'); } catch { console.log('Fails as expected 2'); } + + { + const unregister = register(); + + const { message } = await import('./file?4'); + console.log(message); + + await unregister(); + } `, ...tsFiles, }); - onTestFinish(async () => await fixture.rm()); - const { stdout } = await execaNode(path.join(fixture.path, 'register.mjs'), [], { + const { stdout } = await execaNode(fixture.getPath('register.mjs'), [], { nodePath: node.path, nodeOptions: [], }); - expect(stdout).toBe('Fails as expected 1\nfoo bar\nFails as expected 2'); + expect(stdout).toBe('Fails as expected 1\nfoo bar\nFails as expected 2\nfoo bar'); }); - test('onImport', async ({ onTestFinish }) => { - const fixture = await createFixture({ + test('onImport', async () => { + await using fixture = await createFixture({ 'package.json': JSON.stringify({ type: 'module' }), 'register.mjs': ` import { register } from ${JSON.stringify(tsxEsmApiPath)}; @@ -246,9 +249,8 @@ export default testSuite(({ describe }, node: NodeApis) => { `, ...tsFiles, }); - onTestFinish(async () => await fixture.rm()); - const { stdout } = await execaNode(path.join(fixture.path, 'register.mjs'), [], { + const { stdout } = await execaNode(fixture.getPath('register.mjs'), [], { nodePath: node.path, nodeOptions: [], }); @@ -258,8 +260,8 @@ export default testSuite(({ describe }, node: NodeApis) => { // add CJS test describe('tsImport()', ({ test }) => { - test('module', async ({ onTestFinish }) => { - const fixture = await createFixture({ + test('module', async () => { + await using fixture = await createFixture({ 'package.json': JSON.stringify({ type: 'module' }), 'import.mjs': ` import { tsImport } from ${JSON.stringify(tsxEsmApiPath)}; @@ -281,17 +283,16 @@ export default testSuite(({ describe }, node: NodeApis) => { `, ...tsFiles, }); - onTestFinish(async () => await fixture.rm()); - const { stdout } = await execaNode(path.join(fixture.path, 'import.mjs'), [], { + const { stdout } = await execaNode(fixture.getPath('import.mjs'), [], { nodePath: node.path, nodeOptions: [], }); expect(stdout).toBe('Fails as expected 1\nfoo bar\nfoo bar\nFails as expected 2'); }); - test('commonjs', async ({ onTestFinish }) => { - const fixture = await createFixture({ + test('commonjs', async () => { + await using fixture = await createFixture({ 'package.json': JSON.stringify({ type: 'module' }), 'import.cjs': ` const { tsImport } = require(${JSON.stringify(tsxEsmApiCjsPath)}); @@ -315,17 +316,16 @@ export default testSuite(({ describe }, node: NodeApis) => { `, ...tsFiles, }); - onTestFinish(async () => await fixture.rm()); - const { stdout } = await execaNode(path.join(fixture.path, 'import.cjs'), [], { + const { stdout } = await execaNode(fixture.getPath('import.cjs'), [], { nodePath: node.path, nodeOptions: [], }); expect(stdout).toBe('Fails as expected 1\nfoo bar\nfoo bar\nFails as expected 2'); }); - test('namespace allows async nested calls', async ({ onTestFinish }) => { - const fixture = await createFixture({ + test('namespace allows async nested calls', async () => { + await using fixture = await createFixture({ 'package.json': JSON.stringify({ type: 'module' }), 'import.mjs': ` import { tsImport } from ${JSON.stringify(tsxEsmApiPath)}; @@ -335,17 +335,16 @@ export default testSuite(({ describe }, node: NodeApis) => { 'file.ts': 'import(\'./foo.ts\')', 'foo.ts': 'console.log(\'foo\' as string)', }); - onTestFinish(async () => await fixture.rm()); - const { stdout } = await execaNode(path.join(fixture.path, 'import.mjs'), [], { + const { stdout } = await execaNode(fixture.getPath('import.mjs'), [], { nodePath: node.path, nodeOptions: [], }); expect(stdout).toBe('Fails as expected\nfoo'); }); - test('onImport', async ({ onTestFinish }) => { - const fixture = await createFixture({ + test('onImport', async () => { + await using fixture = await createFixture({ 'package.json': JSON.stringify({ type: 'module' }), 'import.mjs': ` import { tsImport } from ${JSON.stringify(tsxEsmApiPath)}; @@ -359,9 +358,8 @@ export default testSuite(({ describe }, node: NodeApis) => { 'file.ts': 'import(\'./foo.ts\')', 'foo.ts': 'console.log(\'foo\' as string)', }); - onTestFinish(async () => await fixture.rm()); - const { stdout } = await execaNode(path.join(fixture.path, 'import.mjs'), [], { + const { stdout } = await execaNode(fixture.getPath('import.mjs'), [], { nodePath: node.path, nodeOptions: [], }); diff --git a/tests/specs/cli.ts b/tests/specs/cli.ts index ecae2ff39..1acf74b2c 100644 --- a/tests/specs/cli.ts +++ b/tests/specs/cli.ts @@ -1,4 +1,3 @@ -import path from 'node:path'; import { setTimeout } from 'timers/promises'; import { testSuite, expect } from 'manten'; import { createFixture } from 'fs-fixture'; @@ -30,7 +29,7 @@ export default testSuite(({ describe }, node: NodeApis) => { test('doesn\'t show version with file', async () => { const tsxProcess = await tsx([ - path.join(fixture.path, 'log-argv.ts'), + fixture.getPath('log-argv.ts'), '--version', ]); @@ -53,7 +52,7 @@ export default testSuite(({ describe }, node: NodeApis) => { test('doesn\'t show help with file', async () => { const tsxProcess = await tsx([ - path.join(fixture.path, 'log-argv.ts'), + fixture.getPath('log-argv.ts'), '--help', ]); @@ -108,8 +107,8 @@ export default testSuite(({ describe }, node: NodeApis) => { // https://github.com/nodejs/node/issues/48467 && node.version !== '20.0.0' ) { - test('Node.js test runner', async ({ onTestFinish }) => { - const fixture = await createFixture({ + test('Node.js test runner', async () => { + await using fixture = await createFixture({ 'test.ts': ` import { test } from 'node:test'; import assert from 'assert'; @@ -119,7 +118,6 @@ export default testSuite(({ describe }, node: NodeApis) => { }); `, }); - onTestFinish(async () => await fixture.rm()); const tsxProcess = await tsx( [ @@ -188,7 +186,7 @@ export default testSuite(({ describe }, node: NodeApis) => { test('Propagates signal', async () => { const exitCode = Math.floor(Math.random() * 100); const tsxProcess = await tsx([ - path.join(fixture.path, 'propagates-signal.js'), + fixture.getPath('propagates-signal.js'), exitCode.toString(), ]); expect(tsxProcess.exitCode).toBe(exitCode); @@ -198,7 +196,7 @@ export default testSuite(({ describe }, node: NodeApis) => { for (const signal of signals) { test(signal, async ({ onTestFail }) => { const tsxProcess = tsx([ - path.join(fixture.path, 'catch-signals.js'), + fixture.getPath('catch-signals.js'), ]); tsxProcess.stdout!.once('data', () => { @@ -240,7 +238,7 @@ export default testSuite(({ describe }, node: NodeApis) => { test('Kills child when unresponsive (infinite loop)', async () => { const tsxProcess = tsx([ - path.join(fixture.path, 'infinite-loop.js'), + fixture.getPath('infinite-loop.js'), ]); const childPid = await new Promise((resolve) => { @@ -272,7 +270,7 @@ export default testSuite(({ describe }, node: NodeApis) => { test('Doesn\'t kill child when responsive (ignores signal)', async () => { const tsxProcess = tsx([ - path.join(fixture.path, 'ignores-signals.js'), + fixture.getPath('ignores-signals.js'), ]); const childPid = await new Promise((resolve) => { @@ -312,7 +310,7 @@ export default testSuite(({ describe }, node: NodeApis) => { const output = await ptyShell( [ // Windows doesn't support shebangs - `${node.path} ${tsxPath} ${path.join(fixture.path, 'keep-alive.js')}\r`, + `${node.path} ${tsxPath} ${fixture.getPath('keep-alive.js')}\r`, stdout => stdout.includes('READY') && CtrlC, `echo EXIT_CODE: ${isWindows ? '$LastExitCode' : '$?'}\r`, ], @@ -324,7 +322,7 @@ export default testSuite(({ describe }, node: NodeApis) => { const output = await ptyShell( [ // Windows doesn't support shebangs - `${node.path} ${tsxPath} ${path.join(fixture.path, 'catch-signals.js')}\r`, + `${node.path} ${tsxPath} ${fixture.getPath('catch-signals.js')}\r`, stdout => stdout.includes('READY') && CtrlC, `echo EXIT_CODE: ${isWindows ? '$LastExitCode' : '$?'}\r`, ], @@ -347,7 +345,7 @@ export default testSuite(({ describe }, node: NodeApis) => { const output = await ptyShell( [ // Windows doesn't support shebangs - `${node.path} ${tsxPath} ${path.join(fixture.path, 'infinite-loop.js')}\r`, + `${node.path} ${tsxPath} ${fixture.getPath('infinite-loop.js')}\r`, stdout => /^\r?\d+$/.test(stdout) && CtrlC, `echo EXIT_CODE: ${isWindows ? '$LastExitCode' : '$?'}\r`, ], @@ -359,8 +357,8 @@ export default testSuite(({ describe }, node: NodeApis) => { }); }); - test('relays ipc message to child and back', async ({ onTestFinish }) => { - const fixture = await createFixture({ + test('relays ipc message to child and back', async () => { + await using fixture = await createFixture({ 'file.js': ` process.on('message', (received) => { process.send('goodbye'); @@ -369,8 +367,6 @@ export default testSuite(({ describe }, node: NodeApis) => { `, }); - onTestFinish(async () => await fixture.rm()); - const tsxProcess = tsx(['file.js'], { cwd: fixture.path, stdio: ['ipc'], diff --git a/tests/specs/smoke.ts b/tests/specs/smoke.ts index 5e4d8f736..f19a599d8 100644 --- a/tests/specs/smoke.ts +++ b/tests/specs/smoke.ts @@ -459,7 +459,7 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => { console.log(p); }); expect(p.failed).toBe(false); - expect(p.stdout).toMatch(`"import.meta.url":"${pathToFileURL(path.join(fixture.path, 'import-from-js.js'))}"`); + expect(p.stdout).toMatch(`"import.meta.url":"${pathToFileURL(fixture.getPath('import-from-js.js'))}"`); expect(p.stdout).toMatch(`"js":{"cjsContext":${isCommonJs},"default":1,"named":2}`); expect(p.stdout).toMatch('"json":{"default":{"loaded":"json"},"loaded":"json"}'); expect(p.stdout).toMatch('"cjs":{"default":{"named":"named"},"named":"named"}'); @@ -481,7 +481,7 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => { ...files, 'package.json': JSON.stringify({ type: packageType }), - 'import-from-ts.ts': outdent` + 'import-from-ts.ts': ({ fixturePath }) => outdent` import assert from 'assert'; import { expectErrors } from './expect-errors'; @@ -503,7 +503,11 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => { import './js/index.js?query=123'; import './js/index'; import './js/'; - import { FIXTURE_PATH }; + import ${JSON.stringify( + packageType === 'module' + ? new URL('js/index.js', pathToFileURL(fixturePath)).toString() + : path.resolve(fixturePath, 'js/index.js'), + )}; // No double .default.default in Dynamic Import import('./js/index.js').then(m => { @@ -640,17 +644,6 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => { // Comment at EOF: could be a sourcemap declaration. Edge case for inserting functions here `.trim(), }); - - const importFromTs = await fixture.readFile('import-from-ts.ts', 'utf8'); - const importFromTsWithAbsolutePath = importFromTs.toString().replace( - '{ FIXTURE_PATH }', - JSON.stringify( - packageType === 'module' - ? new URL('js/index.js', pathToFileURL(`${fixture.path}/`)).toString() - : path.resolve(fixture.path, 'js/index.js'), - ), - ); - await fixture.writeFile('import-from-ts.ts', importFromTsWithAbsolutePath); onFinish(async () => await fixture.rm()); test('import all', async ({ onTestFail }) => { @@ -660,7 +653,7 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => { console.log(p); }); expect(p.failed).toBe(false); - expect(p.stdout).toMatch(`"import.meta.url":"${pathToFileURL(path.join(fixture.path, 'import-from-ts.ts'))}"`); + expect(p.stdout).toMatch(`"import.meta.url":"${pathToFileURL(fixture.getPath('import-from-ts.ts'))}"`); expect(p.stdout).toMatch(`"js":{"cjsContext":${isCommonJs},"default":1,"named":2}`); expect(p.stdout).toMatch('"json":{"default":{"loaded":"json"},"loaded":"json"}'); expect(p.stdout).toMatch('"cjs":{"default":{"named":"named"},"named":"named"}'); @@ -678,7 +671,7 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => { }); test('tsconfig', async ({ onTestFail }) => { - const pTsconfig = await tsx(['index.tsx'], path.join(fixture.path, 'tsconfig')); + const pTsconfig = await tsx(['index.tsx'], fixture.getPath('tsconfig')); onTestFail((error) => { console.error(error); console.log(pTsconfig); @@ -689,7 +682,7 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => { }); test('custom tsconfig', async ({ onTestFail }) => { - const pTsconfigAllowJs = await tsx(['--tsconfig', 'tsconfig-allowJs.json', 'jsx.jsx'], path.join(fixture.path, 'tsconfig')); + const pTsconfigAllowJs = await tsx(['--tsconfig', 'tsconfig-allowJs.json', 'jsx.jsx'], fixture.getPath('tsconfig')); onTestFail((error) => { console.error(error); console.log(pTsconfigAllowJs);