Skip to content

Commit

Permalink
test: upgrade fs-fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed May 10, 2024
1 parent 58e6d68 commit d6a5f05
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 97 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 6 additions & 6 deletions pnpm-lock.yaml

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

110 changes: 54 additions & 56 deletions tests/specs/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import path from 'node:path';
import { execaNode } from 'execa';
import { testSuite, expect } from 'manten';
import { createFixture } from 'fs-fixture';
Expand Down Expand Up @@ -34,23 +33,22 @@ 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],
});
expect(stdout).toContain('foo bar');
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 {
Expand Down Expand Up @@ -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: [],
});
Expand All @@ -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)});
Expand All @@ -116,27 +113,25 @@ 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: [],
});

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,
Expand All @@ -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],
});
Expand All @@ -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';
Expand All @@ -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: [],
});
Expand All @@ -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)};
Expand All @@ -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)};
Expand All @@ -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: [],
});
Expand All @@ -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)};
Expand All @@ -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)});
Expand All @@ -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)};
Expand All @@ -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)};
Expand All @@ -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: [],
});
Expand Down
Loading

0 comments on commit d6a5f05

Please sign in to comment.