Skip to content

Commit

Permalink
test: refactor to resolve from cwd
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Sep 14, 2023
1 parent 67a776a commit 7b8503c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 21 deletions.
36 changes: 15 additions & 21 deletions tests/specs/json.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import path from 'path';
import { testSuite, expect } from 'manten';
import { createFixture } from 'fs-fixture';
import type { NodeApis } from '../utils/tsx';
Expand All @@ -17,66 +16,61 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
onFinish(async () => await fixture.rm());

describe('full path', async ({ test }) => {
const importPath = path.join(fixture.path, 'index.json');
test('Load', async () => {
const nodeProcess = await node.load(fixture.path);
const nodeProcess = await node.loadFile(fixture.path, './index.json');
expect(nodeProcess.stdout).toBe('');
expect(nodeProcess.exitCode).toBe(0);
});

test('Import', async () => {
const nodeProcess = await node.import(importPath);
expect(nodeProcess.stdout).toBe('{"default":{"loaded":"json"},"loaded":"json"}');
const nodeProcess = await node.importFile(fixture.path, './index.json');
expect(nodeProcess.stdout).toMatch('default: { loaded: \'json\' }');
expect(nodeProcess.stderr).toBe('');
});

test('Require', async () => {
const nodeProcess = await node.require(importPath);
expect(nodeProcess.stdout).toBe('{"loaded":"json"}');
const nodeProcess = await node.requireFile(fixture.path, './index.json');
expect(nodeProcess.stdout).toBe('{ loaded: \'json\' }');
expect(nodeProcess.stderr).toBe('');
});
});

describe('extensionless', ({ test }) => {
const importPath = path.join(fixture.path, 'index');

test('Load', async () => {
const nodeProcess = await node.load(importPath);
const nodeProcess = await node.loadFile(fixture.path, './index');
expect(nodeProcess.stdout).toBe('');
expect(nodeProcess.exitCode).toBe(0);
});

test('Import', async () => {
const nodeProcess = await node.import(importPath);
expect(nodeProcess.stdout).toBe('{"default":{"loaded":"json"},"loaded":"json"}');
const nodeProcess = await node.importFile(fixture.path, './index');
expect(nodeProcess.stdout).toMatch('default: { loaded: \'json\' }');
expect(nodeProcess.stderr).toBe('');
});

test('Require', async () => {
const nodeProcess = await node.require(importPath);
expect(nodeProcess.stdout).toBe('{"loaded":"json"}');
const nodeProcess = await node.requireFile(fixture.path, './index');
expect(nodeProcess.stdout).toBe('{ loaded: \'json\' }');
expect(nodeProcess.stderr).toBe('');
});
});

describe('directory', ({ test }) => {
const importPath = fixture.path;

test('Load', async () => {
const nodeProcess = await node.load(importPath);
const nodeProcess = await node.loadFile(fixture.path, '.');
expect(nodeProcess.stdout).toBe('');
expect(nodeProcess.exitCode).toBe(0);
});

test('Import', async () => {
const nodeProcess = await node.import(importPath);
expect(nodeProcess.stdout).toBe('{"default":{"loaded":"json"},"loaded":"json"}');
const nodeProcess = await node.importFile(fixture.path, '.');
expect(nodeProcess.stdout).toMatch('default: { loaded: \'json\' }');
expect(nodeProcess.stderr).toBe('');
});

test('Require', async () => {
const nodeProcess = await node.require(importPath);
expect(nodeProcess.stdout).toBe('{"loaded":"json"}');
const nodeProcess = await node.requireFile(fixture.path, '.');
expect(nodeProcess.stdout).toBe('{ loaded: \'json\' }');
expect(nodeProcess.stderr).toBe('');
});
});
Expand Down
49 changes: 49 additions & 0 deletions tests/utils/tsx.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path';
import fs from 'fs/promises';
import { fileURLToPath } from 'url';
import { execaNode } from 'execa';
import getNode from 'get-node';
Expand Down Expand Up @@ -107,6 +108,54 @@ export async function createNode(
cwd: fixturePath,
});
},

loadFile(
cwd: string,
filePath: string,
options?: {
args?: string[];
},
) {
return this.tsx(
{
args: [
...(options?.args ?? []),
filePath,
],
cwd,
},
);
},

async importFile(
cwd: string,
importFrom: string,
fileExtension = '.mjs',
) {
const fileName = `_${Math.random().toString(36).slice(2)}${fileExtension}`;
const filePath = path.resolve(cwd, fileName);
await fs.writeFile(filePath, `import * as _ from '${importFrom}';console.log(_)`);
try {
return await this.loadFile(cwd, filePath);
} finally {
await fs.rm(filePath);
}
},

async requireFile(
cwd: string,
requireFrom: string,
fileExtension = '.cjs',
) {
const fileName = `_${Math.random().toString(36).slice(2)}${fileExtension}`;
const filePath = path.resolve(cwd, fileName);
await fs.writeFile(filePath, `const _ = require('${requireFrom}');console.log(_)`);
try {
return await this.loadFile(cwd, filePath);
} finally {
await fs.rm(filePath);
}
},
};
}

Expand Down

0 comments on commit 7b8503c

Please sign in to comment.