Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ root = true
end_of_line = lf
charset = utf-8

[*.{js,cjs,mjs,ts}]
[*.{js,cjs,mjs,ts,cts,mts}]
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
Expand Down
1 change: 0 additions & 1 deletion .eslintignore

This file was deleted.

115 changes: 0 additions & 115 deletions .eslintrc.js

This file was deleted.

9 changes: 0 additions & 9 deletions .mocharc.js

This file was deleted.

18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import { functionToTest } from '../src/SomethingToTest';

// Adapt based on your testing framework. This example uses Mocha and Chai's syntax.

it('should produce a success response', () => {
it('should produce a success response', async () => {
const server = newServer({
get: ['/my/url', {
// status: 200 is the default
Expand All @@ -65,10 +65,9 @@ it('should produce a success response', () => {
server.install(/* optional context; defaults to globalThis */);

// Do something that send()s an XMLHttpRequest to '/my/url' and returns a Promise
return functionToTest().then((result) => {
// This assumes the returned Promise resolves to the parsed JSON response
assert.equal(result.message, 'Success!');
});
// that resolves to the parsed JSON response
const result = await functionToTest();
assert.equal(result.message, 'Success!');
} finally {
// Restore the original XMLHttpRequest
server.remove();
Expand Down Expand Up @@ -178,7 +177,7 @@ import { functionToTest } from '../src/SomethingToTest';

// Adapt based on your testing framework. This example uses Mocha and Chai's syntax.

it('should produce a success response', () => {
it('should produce a success response', async () => {
// Get a "local" MockXhr subclass
const MockXhr = newMockXhr();

Expand All @@ -194,10 +193,9 @@ it('should produce a success response', () => {
global.XMLHttpRequest = MockXhr;

// Do something that send()s an XMLHttpRequest to '/my/url' and returns a Promise
return functionToTest().then((result) => {
// This assumes the returned Promise resolves to the parsed JSON response
assert.equal(result.message, 'Success!');
});
// that resolves to the parsed JSON response
const result = await functionToTest();
assert.equal(result.message, 'Success!');
} finally {
// Restore the original XMLHttpRequest
delete global.XMLHttpRequest;
Expand Down
6 changes: 0 additions & 6 deletions build/.eslintrc.js

This file was deleted.

119 changes: 119 additions & 0 deletions build/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { openSync, readFileSync, readdirSync, renameSync, writeSync } from 'node:fs';
import { rollup } from 'rollup';
import typescript from '@rollup/plugin-typescript';

import type { ModuleFormat, OutputOptions, RollupBuild } from 'rollup';
import { join } from 'node:path';

const packageRoot = join(import.meta.dirname, '..');

const version = process.env.VERSION ??
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
JSON.parse(readFileSync(join(packageRoot, 'package.json'), 'utf8')).version as string;

const banner = `/**
* mock-xmlhttprequest v${version}
* (c) ${new Date().getFullYear()} Bertrand Guay-Paquet
* @license MIT
*/
`;

const entryPoint = join(packageRoot, 'src', 'index.ts');

const commonOutputOptions: Partial<OutputOptions> = {
banner,
generatedCode: 'es2015',
preserveModules: true,
};

const tsconfigPath = join(packageRoot, 'tsconfig.json');

const outDir = join(packageRoot, 'dist');

const outputs = [
{
format: 'esm' as ModuleFormat,
jsExt: 'mjs',
dtsExt: 'mts',
}, {
format: 'cjs' as ModuleFormat,
jsExt: 'cjs',
dtsExt: 'cts',
},
];

await build();

async function build() {
let buildFailed = false;
for (const output of outputs) {
let codeBundle: RollupBuild | undefined;
try {
// rollup() currently produces this warning:
// [plugin typescript] @rollup/plugin-typescript TS5096: Option 'allowImportingTsExtensions'
// can only be used when either 'noEmit' or 'emitDeclarationOnly' is set.
// However the actual outputs are fine. We'll ignore this warning for now and re-evaluate when
// TypeScript 5.7 is released with the new "Path Rewriting for Relative Paths" feature.
// See also https://github.com/rollup/plugins/discussions/1536
const dir = join(outDir, output.format);
const outputOptions: OutputOptions = {
...commonOutputOptions,
format: output.format,
preserveModules: true,
dir,
entryFileNames: `[name].${output.jsExt}`,
};

codeBundle = await rollup({
input: entryPoint,
plugins: [
typescript({
tsconfig: tsconfigPath,
declarationDir: outputOptions.dir,
compilerOptions: {
declaration: true,
},
}),
],
});

await codeBundle.write(outputOptions);

fixupDeclarationFiles(dir, output.dtsExt);
} catch (e) {
buildFailed = true;
console.error(e);
} finally {
if (codeBundle) {
await codeBundle.close();
}
}
}

process.exit(buildFailed ? 1 : 0);
}

function fixupDeclarationFiles(dir: string, newFileExtension: string) {
const files = readdirSync(dir, { recursive: true }) as string[];
for (const file of files) {
if (file.endsWith('.d.ts')) {
const filePath = join(dir, file);

let fileContent = readFileSync(filePath, 'utf8');

// Change the '.ts' file extension in imports of the declaration files
// e.g. import ... from './RequestData.ts'; => import ... from './RequestData.mts';
fileContent = fileContent.replaceAll(/(['"][^'"]*\.)ts(['"])/g, `$1${newFileExtension}$2`);

const fd = openSync(filePath, 'w');

// Add the copyright header
writeSync(fd, banner);
writeSync(fd, '\n');
writeSync(fd, fileContent);

// Rename the declaration file to the new file extension
renameSync(filePath, join(dir, `${file.slice(0, -2)}${newFileExtension}`));
}
}
}
56 changes: 0 additions & 56 deletions build/rollup.config.ts

This file was deleted.

10 changes: 0 additions & 10 deletions build/tsconfig.dts.json

This file was deleted.

Loading