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 4d5e97d commit 5969aa6
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 393 deletions.
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
7 changes: 2 additions & 5 deletions list.ts
Original file line number Diff line number Diff line change
@@ -1,11 +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;

export interface ListParams {
Expand All @@ -25,7 +22,7 @@ export async function list(params: ListParams): Promise<Scenario[]> {
await import(resolve(file));
}
}
return seenScenarios;
return global.scenarioTesterSeenScenarios;
}

export async function printList(params: ListParams) {
Expand Down
24 changes: 14 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
"bin": {
"scenario-tester": "./build/cli.js"
},
"type": "module",
"scripts": {
"prepare": "tsc",
"prepare": "yarn build",
"build": "concurrently --no-color \"npm:build:*\" --names \"build:\"",
"build:esm": "tsc -p tsconfig.esm.json && tsconfig-to-dual-package tsconfig.esm.json",
"build:cjs": "tsc -p tsconfig.cjs.json && tsconfig-to-dual-package tsconfig.cjs.json",
"build:types": "tsc -p tsconfig.types.json",
"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 +54,23 @@
"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",
"typedoc": "^0.24.8",
"typescript": "^5.1.6"
},
"exports": {
".": {
"import": "./build/index.js"
"import": "./build/esm/index.js"
},
"./*": {
"types": "./build/*.d.ts",
"import": "./build/*.js",
"default": "./build/*.js"
"types": "./build/types/*.d.ts",
"import": "./build/esm/*.js",
"default": "./build/cjs/*.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('../build/cjs/index');
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', ['build/esm/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', ['build/esm/cli.js', 'list', '--files', 'tests/test.mjs', '--matrix'])

const { stdout } = result;
assert.deepEqual(
Expand Down
4 changes: 2 additions & 2 deletions tests/test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Project } from 'fixturify-project';
import { Scenarios } from '../index.js';
import { Scenarios } from '../';
import type { PreparedApp } from '../index.js';
import Qunit from 'qunit';
import execa from 'execa';
Expand Down Expand Up @@ -79,7 +79,7 @@ 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', ['build/esm/cli.js', 'list', '--files', './tests/test.cjs', '--matrix'])

const { stdout } = result;
assert.deepEqual(
Expand Down
7 changes: 7 additions & 0 deletions tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./build/cjs",
"module": "commonjs",
}
}
7 changes: 7 additions & 0 deletions tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./build/esm",
"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
Loading

0 comments on commit 5969aa6

Please sign in to comment.