Skip to content

Commit

Permalink
feat(intercept): update project to a module
Browse files Browse the repository at this point in the history
  • Loading branch information
tmsns committed Dec 5, 2023
1 parent a31d355 commit 0ef9cb5
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 37 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"start:build": "node packages/dev-server/dist/bin.js --root-dir _site --open",
"test": "npm run test:node && npm run test:browser && node scripts/workspaces-scripts-bin.mjs test:ci",
"test:browser": "npm run test:browser --workspaces --if-present",
"test:node": "mocha \"packages/!(*test-runner-selenium|*test-runner-webdriver)/test/**/*.test.{ts,js,mjs,cjs}\"",
"test:node": "mocha \"packages/!(*test-runner-selenium|*test-runner-webdriver|*test-runner-intercept)/test/**/*.test.{ts,js,mjs,cjs}\" && TS_NODE_PROJECT='./tsconfig.node-16-base.json' mocha \"packages/test-runner-intercept/test/**/*.test.{ts,js,mjs,cjs}\" --loader=ts-node/esm",
"types": "wireit",
"update": "npm run update:mjs-dts-entrypoints && npm run update:tsconfigs",
"update-dependency": "node scripts/update-dependency.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/test-runner-intercept/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ await interceptModule(new URL(import.meta.resolve('../src/library-to-intercept.j
```js
// web-test-runner.config.mjs
import { interceptModulePlugin } from '@web/test-runner-intercept/plugin';
import { interceptModulePlugin } from '@web/test-runner-intercept/plugin.js';

export default {
plugins: [interceptModulePlugin()],
Expand Down
18 changes: 8 additions & 10 deletions packages/test-runner-intercept/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@
"author": "modern-web",
"homepage": "https://github.com/modernweb-dev/web/tree/master/packages/test-runner-intercept",
"main": "browser/index.js",
"type": "module",
"exports": {
".": {
"types": "./browser/index.d.ts",
"default": "./browser/index.mjs"
"import": "./browser/index.js"
},
"./plugin": {
"types": "./plugin.d.ts",
"import": "./plugin.mjs",
"require": "./dist/interceptModulePlugin.js"
"./plugin.js": {
"types": "./dist/interceptModulePlugin.d.ts",
"import": "./dist/interceptModulePlugin.js"
}
},
"engines": {
"node": ">=18.0.0"
},
"scripts": {
"build": "tsc",
"test": "mocha test/**/*.test.ts --require ts-node/register",
"test:watch": "mocha test/**/*.test.ts --require ts-node/register --watch --watch-files src,test"
"test": "mocha test/**/*.test.ts --loader=ts-node/esm",
"test:watch": "mocha test/**/*.test.ts --loader ts-node/esm --watch --watch-files src,test"
},
"files": [
"*.d.ts",
Expand All @@ -57,8 +57,6 @@
},
"devDependencies": {
"@web/test-runner-chrome": "^0.15.0",
"@web/test-runner-core": "^0.13.0",
"chai": "^4.2.0",
"mocha": "^10.2.0"
"@web/test-runner-core": "^0.13.0"
}
}
2 changes: 1 addition & 1 deletion packages/test-runner-intercept/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { interceptModulePlugin } from './interceptModulePlugin';
export { interceptModulePlugin } from './interceptModulePlugin.js';
4 changes: 2 additions & 2 deletions packages/test-runner-intercept/src/interceptModulePlugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Plugin } from '@web/dev-server-core';
import { parse } from 'es-module-lexer';
import { createResolveImport, ResolveImport } from './createResolveImport';
import { stripColor } from './stripColor';
import { createResolveImport, ResolveImport } from './createResolveImport.js';
import { stripColor } from './stripColor.js';

/**
* Plugin that allows the interception of modules
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from '../chai.js';
import { interceptModule } from '../../../browser/index.mjs';
import { interceptModule } from '../../../browser/index.js';

const timeLibrary = await interceptModule('time-library/hour');
const { getTimeOfDay } = await import('./fixture/getTimeOfDay.js');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { interceptModule } from '../../../browser/index.mjs';
import { interceptModule } from '../../../browser/index.js';

await interceptModule('/inexistent-module.js');
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { interceptModule } from '../../../browser/index.mjs';
import { interceptModule } from '../../../browser/index.js';

await interceptModule('./file.js');
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from '../chai.js';
import { interceptModule } from '../../../browser/index.mjs';
import { interceptModule } from '../../../browser/index.js';

const path = new URL(import.meta.resolve('./fixture/time-library.js')).pathname;
const timeLibrary = await interceptModule(path);
Expand Down
17 changes: 10 additions & 7 deletions packages/test-runner-intercept/test/interceptModulePlugin.test.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
import path from 'path';
import { fileURLToPath } from 'url';
import { runTests } from '@web/test-runner-core/test-helpers';
import { chromeLauncher } from '@web/test-runner-chrome';
import { nodeResolvePlugin } from '@web/dev-server';

import { interceptModulePlugin } from '../src/interceptModulePlugin';
import { interceptModulePlugin } from '../src/interceptModulePlugin.js';
import { expect } from 'chai';

const dirname = fileURLToPath(new URL('.', import.meta.url));

describe('interceptModulePlugin', function test() {
this.timeout(20000);

it('can intercept server relative modules', async () => {
await runTests({
files: [path.join(__dirname, 'fixtures', 'server-relative', 'browser-test.js')],
files: [path.join(dirname, 'fixtures', 'server-relative', 'browser-test.js')],
browsers: [chromeLauncher()],
plugins: [interceptModulePlugin(), nodeResolvePlugin('', false, {})],
});
});

it('can intercept bare modules', async () => {
const rootDir = path.resolve(__dirname, 'fixtures', 'bare', 'fixture');
const rootDir = path.resolve(dirname, 'fixtures', 'bare', 'fixture');
// Define the bare module as duped to force nodeResolve to use the passed rootDir instead of the cwd
const dedupe = (importee: string) => importee === 'time-library/hour';

await runTests({
files: [path.join(__dirname, 'fixtures', 'bare', 'browser-test.js')],
files: [path.join(dirname, 'fixtures', 'bare', 'browser-test.js')],
browsers: [chromeLauncher()],
plugins: [interceptModulePlugin(), nodeResolvePlugin(rootDir, false, { dedupe })],
});
Expand All @@ -32,7 +35,7 @@ describe('interceptModulePlugin', function test() {
it('throws when trying to intercept without the plugin', async () => {
const { sessions } = await runTests(
{
files: [path.join(__dirname, 'fixtures', 'server-relative', 'browser-test.js')],
files: [path.join(dirname, 'fixtures', 'server-relative', 'browser-test.js')],
browsers: [chromeLauncher()],
plugins: [nodeResolvePlugin('', false, {})],
},
Expand All @@ -50,7 +53,7 @@ describe('interceptModulePlugin', function test() {
it('throws when trying to intercept an inexistent module', async () => {
const { sessions } = await runTests(
{
files: [path.join(__dirname, 'fixtures', 'inexistent', 'browser-test.js')],
files: [path.join(dirname, 'fixtures', 'inexistent', 'browser-test.js')],
browsers: [chromeLauncher()],
plugins: [interceptModulePlugin(), nodeResolvePlugin('', false, {})],
},
Expand All @@ -68,7 +71,7 @@ describe('interceptModulePlugin', function test() {
it('throws when trying to intercept a relative module', async () => {
const { sessions } = await runTests(
{
files: [path.join(__dirname, 'fixtures', 'relative', 'browser-test.js')],
files: [path.join(dirname, 'fixtures', 'relative', 'browser-test.js')],
browsers: [chromeLauncher()],
plugins: [interceptModulePlugin(), nodeResolvePlugin('', false, {})],
},
Expand Down
14 changes: 4 additions & 10 deletions packages/test-runner-intercept/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// Don't edit this file directly. It is generated by generate-ts-configs script

{
"extends": "../../tsconfig.node-base.json",
"compilerOptions": {
"module": "commonjs",
"module": "node16",
"moduleResolution": "node16",
"outDir": "./dist",
"rootDir": "./src",
"rootDir": "./",
"composite": true,
"allowJs": true
},
Expand Down Expand Up @@ -34,11 +33,6 @@
],
"include": [
"src",
"types"
"test/**/*.ts"
],
"exclude": [
"src/browser",
"tests",
"dist"
]
}
7 changes: 7 additions & 0 deletions tsconfig.node-16-base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.node-base.json",
"compilerOptions": {
"module": "node16",
"moduleResolution": "node16",
}
}
2 changes: 1 addition & 1 deletion workspace-packages.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const packages = [
{ name: 'test-runner-browserstack', type: 'ts', environment: 'node' },
{ name: 'test-runner-coverage-v8', type: 'ts', environment: 'node' },
{ name: 'test-runner-commands', type: 'ts', environment: 'node' },
{ name: 'test-runner-intercept', type: 'ts', environment: 'node' },
{ name: 'test-runner-intercept', ignoreTsConfig: true },
{ name: 'test-runner-junit-reporter', type: 'ts', environment: 'node' },
{ name: 'test-runner-mocha', type: 'ts', environment: 'browser' },
{ name: 'test-runner-saucelabs', type: 'ts', environment: 'node' },
Expand Down

0 comments on commit 0ef9cb5

Please sign in to comment.