Skip to content

Commit

Permalink
compile to esm
Browse files Browse the repository at this point in the history
  • Loading branch information
mansona committed Sep 1, 2023
1 parent a5898f4 commit 1ea671e
Show file tree
Hide file tree
Showing 16 changed files with 603 additions and 35 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/node_modules
/*.d.ts
/*.js
/*.js.map
/*.js.map
/build/
4 changes: 2 additions & 2 deletions cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import yargs from 'yargs';

yargs
yargs(process.argv.slice(2))
.demandCommand(1, 'Use one of the above commands')
.command(
'list',
Expand All @@ -22,7 +22,7 @@ yargs
.array('require')
.option('matrix', { type: 'string' }),
async argv => {
let mod = await import('./list');
let mod = await import('./list.js');
await mod.printList(argv);
}
)
Expand Down
13 changes: 9 additions & 4 deletions list.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { Scenario, seenScenarios } from '.';
import { sync as globSync } from 'glob';
import { Scenario, seenScenarios } from './index.js';
import glob from 'glob';
import { resolve } from 'path';
import { format } from 'util';

import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);

const { sync: globSync } = glob;

export interface ListParams {
files: string[];
require: string[] | undefined;
Expand All @@ -12,12 +17,12 @@ export interface ListParams {
export async function list(params: ListParams): Promise<Scenario[]> {
if (params.require) {
for (let r of params.require) {
require(require.resolve(r, { paths: [process.cwd()]}));
await import(require.resolve(r, { paths: [process.cwd()]}));
}
}
for (let pattern of params.files) {
for (let file of globSync(pattern)) {
require(resolve(file));
await import(resolve(file));
}
}
return seenScenarios;
Expand Down
29 changes: 22 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@
"repository": "https://github.com/ef4/scenario-tester",
"version": "2.1.2",
"bin": {
"scenario-tester": "./cli.js"
"scenario-tester": "./build/cli.js"
},
"main": "index.js",
"type": "module",
"scripts": {
"prepare": "tsc",
"start": "tsc --watch",
"test": "qunit test.js"
"test": "concurrently --no-color \"npm:test:*\" --names \"test:\"",
"test:ts": "node --loader ts-node/esm node_modules/.bin/qunit --require ts-node/register tests/test.ts",
"test:mjs": "qunit tests/test.mjs"
},
"files": [
"*.js",
"*.d.ts",
"*.map",
"!test.*"
"build/*.js",
"build/*.d.ts",
"build/*.map",
"!build/tests/*"
],
"dependencies": {
"fixturify-project": "^6.0.0",
Expand All @@ -35,7 +37,20 @@
"@types/qunit": "^2.11.3",
"@types/tmp": "^0.2.0",
"@types/yargs": "^16.0.0",
"concurrently": "^8.2.1",
"execa": "^5.0.1",
"qunit": "^2.18.0",
"ts-node": "^10.9.1",
"typescript": "^5.1.6"
},
"exports": {
".": {
"import": "./build/index.js"
},
"./*": {
"types": "./build/*.d.ts",
"import": "./build/*.js",
"default": "./build/*.js"
}
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
85 changes: 85 additions & 0 deletions tests/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { Scenarios } from 'scenario-tester';
import Qunit from 'qunit';
import execa from 'execa';

function hello1(project) {
project.linkDependency('hello', {
baseDir: './tests/fixtures',
resolveName: 'hello1',
});
}

function hello2(project) {
project.linkDependency('hello', {
baseDir: './tests/fixtures',
resolveName: 'hello',
});
}

const scenarios = Scenarios.fromDir('./tests/fixtures/app').expand({
hello1,
hello2,
});


scenarios.forEachScenario((scenario) => {
Qunit.module(scenario.name, (hooks) => {
hooks.before(async function () {
this.app = await scenario.prepare();
});

Qunit.test(
'yarn test',
async function (assert) {
const result = await this.app.execute('yarn --silent test');
assert.equal(
result.stdout,
`TAP version 13
ok 1 project > createHello
1..1
# pass 1
# skip 0
# todo 0
# fail 0
`
);
}
);

Qunit.test(
'yarn bin inside app',
async function (assert) {
let result = await this.app.execute('yarn --silent bin');
const yarnBin = result.stdout.trimRight();
assert.ok(yarnBin.startsWith(this.app.dir));
result = await this.app.execute('yarn --silent exec which qunit');
assert.ok(result.stdout.startsWith(yarnBin));
}
);

Qunit.test(
'check scenario',
async function (assert) {
let result = await this.app.execute(
`node -p 'require("./index").polyfilled'`
);
assert.equal(
result.stdout.trim(),
('hello1' === scenario.name).toString()
);
}
);
});
});

Qunit.module('cli', () => {
Qunit.test('list', async (assert) => {
const result = await execa('npx', ['.', 'list', '--files', 'tests/test.mjs', '--matrix'])

const { stdout } = result;
assert.deepEqual(
stdout.split('\n'),
['hello1', 'hello2']
);
});
});
30 changes: 14 additions & 16 deletions test.ts → tests/test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { Project } from 'fixturify-project';
import { Scenarios } from './index';
import type { PreparedApp } from './index';
import Qunit = require('qunit');
import child_process = require('child_process');
import { Scenarios } from '../index.js';
import type { PreparedApp } from '../index.js';
import Qunit from 'qunit';
import execa from 'execa';

function hello1(project: Project) {
project.linkDependency('hello', {
baseDir: __dirname + '/fixtures',
baseDir: './tests/fixtures',
resolveName: 'hello1',
});
}

function hello2(project: Project) {
project.linkDependency('hello', {
baseDir: __dirname + '/fixtures',
baseDir: './tests/fixtures',
resolveName: 'hello',
});
}

const scenarios = Scenarios.fromDir(__dirname + '/fixtures/app').expand({
const scenarios = Scenarios.fromDir('./tests/fixtures/app').expand({
hello1,
hello2,
});
Expand Down Expand Up @@ -76,16 +76,14 @@ ok 1 project > createHello
});

Qunit.module('cli', () => {
Qunit.test('list', (assert) => {
Qunit.test('list', async (assert) => {
// I tried to test this using the ts file dirrectly but I couldn't get ts-node/require to work correctly
// so I'm just testing against the compiled esm output
const result = await execa('npx', ['.', 'list', '--files', './build/tests/test.js', '--matrix'])

const { stdout } = result;
assert.deepEqual(
child_process
.execFileSync(
process.execPath,
['cli.js', 'list', '--files', 'test.js', '--matrix'],
{ encoding: 'utf8' }
)
.trimRight()
.split('\n'),
stdout.split('\n'),
['hello1', 'hello2']
);
});
Expand Down
13 changes: 10 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
{
"ts-node": {
"esm": true,
"experimentalSpecifierResolution": "node"
},
"compilerOptions": {
"target": "es2019",
"module": "commonjs",
"outDir": "./build/",
"target": "ES2020",
"module": "ES2020",
"moduleResolution": "node",
"declaration": true,
"esModuleInterop": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"sourceMap": true,
"inlineSources": true,
"strict": true
}
},
"exclude": ["build"]
}
12 changes: 12 additions & 0 deletions tsconfig.types.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"outDir": "./build/types",
"target": "ES2020",
"module": "ES2020",
"moduleResolution": "node",
"declaration": true,
"emitDeclarationOnly": true,
"esModuleInterop": true,
},
"exclude": ["tests", "build"]
}
Loading

0 comments on commit 1ea671e

Please sign in to comment.