Skip to content

Commit

Permalink
Enable multi-build for esm and cjs support
Browse files Browse the repository at this point in the history
  • Loading branch information
mansona committed Oct 27, 2023
1 parent f02fcba commit a92564c
Show file tree
Hide file tree
Showing 11 changed files with 363 additions and 417 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/*.js
/*.js.map
/build/
/dist/
/*.log
/typedoc
!.eslintrc.js
9 changes: 7 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,15 +316,20 @@ export class Scenarios {
}
}

export const seenScenarios: Scenario[] = [];
declare global {
// eslint-disable-next-line no-var
var scenarioTesterSeenScenarios: Scenario[];
}

global.scenarioTesterSeenScenarios = [];

export class Scenario {
constructor(
public name: string,
private callbackCreateProject: CallbackCreateProject,
private mutators: CallbackMutateProject[]
) {
seenScenarios.push(this);
global.scenarioTesterSeenScenarios.push(this);
}

async prepare(outdir?: string): Promise<PreparedApp> {
Expand Down
19 changes: 13 additions & 6 deletions list.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { Scenario, seenScenarios } from './index.js';
import { Scenario } 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;

Expand All @@ -17,15 +15,24 @@ export interface ListParams {
export async function list(params: ListParams): Promise<Scenario[]> {
if (params.require) {
for (let r of params.require) {
await import(require.resolve(r, { paths: [process.cwd()]}));
if(import.meta.url) {
const require = createRequire(import.meta.url);
await import(require.resolve(r, { paths: [process.cwd()]}));
} else {
require(require.resolve(r, { paths: [process.cwd()]}));
}
}
}
for (let pattern of params.files) {
for (let file of globSync(pattern)) {
await import(resolve(file));
if(import.meta.url) {
await import(resolve(file));
} else {
require(resolve(file));
}
}
}
return seenScenarios;
return global.scenarioTesterSeenScenarios;
}

export async function printList(params: ListParams) {
Expand Down
27 changes: 16 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
"repository": "https://github.com/ef4/scenario-tester",
"version": "2.1.2",
"bin": {
"scenario-tester": "./build/cli.js"
"scenario-tester": "./dist/cli.js",
"scenario-tester-esm": "./dist/cli.mjs"
},
"type": "module",
"scripts": {
"prepare": "tsc",
"prepare": "yarn build",
"build": "yarn tsup cli.ts index.ts list.ts output.ts --tsconfig tsconfig.esm.json --dts --format esm,cjs",
"start": "tsc --watch",
"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:ts": "node node_modules/.bin/qunit --require ts-node/register tests/test.ts",
"test:cjs": "qunit tests/test.cjs",
"test:mjs": "qunit tests/test.mjs",
"test:esm": "qunit tests/test.mjs",
"docs": "typedoc index.ts --out typedoc/",
"docs:watch": "yarn docs -- --watch",
"docs:serve": "browser-sync start --server \"typedoc/\" --files \"**/*.html\"",
Expand Down Expand Up @@ -51,22 +52,26 @@
"browser-sync": "^2.29.3",
"concurrently": "^8.2.0",
"eslint": "^8.46.0",
"execa": "^5.0.1",
"eslint-plugin-tsdoc": "^0.2.17",
"execa": "^5.0.1",
"lite-server": "^2.6.1",
"qunit": "^2.18.0",
"typedoc": "^0.24.8",
"ts-node": "^10.9.1",
"tsconfig-to-dual-package": "^1.2.0",
"tsup": "^7.2.0",
"typedoc": "^0.24.8",
"typescript": "^5.1.6"
},
"exports": {
".": {
"import": "./build/index.js"
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.js"
},
"./*": {
"types": "./build/*.d.ts",
"import": "./build/*.js",
"default": "./build/*.js"
"types": "./dist/*.d.ts",
"import": "./dist/*.mjs",
"require": "./dist/*.js"
}
}
}
44 changes: 23 additions & 21 deletions tests/test.cjs
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const index_js_1 = require("./index.js");
const qunit_1 = __importDefault(require("qunit"));
const child_process_1 = __importDefault(require("child_process"));

const { Scenarios } = require('scenario-tester');
const qunit = require("qunit");
const child_process = require("child_process");
const execa = require('execa');

function hello1(project) {
project.linkDependency('hello', {
baseDir: './fixtures',
baseDir: './tests/fixtures',
resolveName: 'hello1',
});
}
function hello2(project) {
project.linkDependency('hello', {
baseDir: './fixtures',
baseDir: './tests/fixtures',
resolveName: 'hello',
});
}
const scenarios = index_js_1.Scenarios.fromDir('./fixtures/app').expand({
const scenarios = Scenarios.fromDir('./tests/fixtures/app').expand({
hello1,
hello2,
});
scenarios.forEachScenario((scenario) => {
qunit_1.default.module(scenario.name, (hooks) => {
qunit.module(scenario.name, (hooks) => {
hooks.before(async function () {
this.app = await scenario.prepare();
});
qunit_1.default.test('yarn test', async function (assert) {
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
Expand All @@ -38,25 +37,28 @@ ok 1 project > createHello
# fail 0
`);
});
qunit_1.default.test('yarn bin inside app', async function (assert) {
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_1.default.test('check scenario', async function (assert) {
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_1.default.module('cli', () => {
qunit_1.default.test('list', (assert) => {
assert.deepEqual(child_process_1.default
.execFileSync(process.execPath, ['cli.js', 'list', '--files', 'test.js', '--matrix'], { encoding: 'utf8' })
.trimRight()
.split('\n'), ['hello1', 'hello2']);
qunit.module('cli', () => {
qunit.test('list', async (assert) => {
const result = await execa('node', ['./dist/cli.js', 'list', '--files', 'tests/test.cjs', '--matrix'])

const { stdout } = result;
assert.deepEqual(
stdout.split('\n'),
['hello1', 'hello2']
);
});
});
//# sourceMappingURL=test.js.map
//# sourceMappingURL=test.js.map
2 changes: 1 addition & 1 deletion tests/test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ ok 1 project > createHello

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

const { stdout } = result;
assert.deepEqual(
Expand Down
8 changes: 3 additions & 5 deletions tests/test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Project } from 'fixturify-project';
import { Scenarios } from '../index.js';
import type { PreparedApp } from '../index.js';
import { Scenarios } from '../';
import type { PreparedApp } from '../';
import Qunit from 'qunit';
import execa from 'execa';

Expand Down Expand Up @@ -77,9 +77,7 @@ ok 1 project > createHello

Qunit.module('cli', () => {
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 result = await execa('node', ['./dist/cli.js', 'list', '--require', 'ts-node/register', '--files', './tests/test.ts', '--matrix'])

const { stdout } = result;
assert.deepEqual(
Expand Down
6 changes: 6 additions & 0 deletions tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "ES2020",
}
}
2 changes: 0 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
"experimentalSpecifierResolution": "node"
},
"compilerOptions": {
"outDir": "./build/",
"target": "ES2020",
"module": "ES2020",
"moduleResolution": "node",
"declaration": true,
"esModuleInterop": true,
Expand Down
12 changes: 0 additions & 12 deletions tsconfig.types.json

This file was deleted.

Loading

0 comments on commit a92564c

Please sign in to comment.