Skip to content

Commit

Permalink
compile to esm
Browse files Browse the repository at this point in the history
  • Loading branch information
mansona committed Oct 26, 2023
1 parent d585d77 commit 3777d75
Show file tree
Hide file tree
Showing 17 changed files with 331 additions and 51 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/*.d.ts
/*.js
/*.js.map
/build/
/*.log
/typedoc
!.eslintrc.js
6 changes: 3 additions & 3 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 Expand Up @@ -53,7 +53,7 @@ yargs
})
.array('require'),
async argv => {
let mod = await import('./output');
let mod = await import('./output.js');
await mod.output(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
6 changes: 3 additions & 3 deletions output.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { removeSync } from 'fs-extra';
import { list, ListParams } from './list';
import fs from 'fs-extra';
import { list, ListParams } from './list.js';

export interface OutputParams extends ListParams {
scenario: string;
Expand All @@ -11,7 +11,7 @@ export async function output(params: OutputParams) {
for (let scenario of scenarios) {
if (scenario.name.indexOf(params.scenario) !== -1) {
process.stdout.write(`Found scenario ${scenario.name}\n`);
removeSync(params.outdir);
fs.removeSync(params.outdir);
await scenario.prepare(params.outdir);
process.stdout.write(`Wrote successfully to ${params.outdir}\n`);
return;
Expand Down
28 changes: 21 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@
"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",
"docs": "typedoc index.ts --out typedoc/",
"docs:watch": "yarn docs -- --watch",
"docs:serve": "browser-sync start --server \"typedoc/\" --files \"**/*.html\"",
"docs:dev": "concurrently \"yarn docs:watch\" \"yarn docs:serve\"",
"lint": "eslint '*.ts'"
},
"files": [
"*.js",
"*.d.ts",
"*.map",
"!test.*"
"build/*.js",
"build/*.d.ts",
"build/*.map",
"!build/tests/*"
],
"dependencies": {
"fixturify-project": "^6.0.0",
Expand All @@ -45,10 +47,22 @@
"browser-sync": "^2.29.3",
"concurrently": "^8.2.0",
"eslint": "^8.46.0",
"execa": "^5.0.1",
"eslint-plugin-tsdoc": "^0.2.17",
"lite-server": "^2.6.1",
"qunit": "^2.18.0",
"typedoc": "^0.24.8",
"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,7 +1,13 @@
{
"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,
Expand All @@ -15,6 +21,7 @@
],
"exclude": [
"node_modules/**/*",
"typedoc"
"typedoc",
"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 3777d75

Please sign in to comment.