Skip to content

Commit

Permalink
test: handle windows backslashes
Browse files Browse the repository at this point in the history
  • Loading branch information
geeksilva97 committed Nov 28, 2024
1 parent cfa3cd6 commit e0ccecc
Showing 1 changed file with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { test } from 'node:test';
import * as fixtures from '../common/fixtures.mjs';
import { spawnSync } from 'node:child_process';
import assert from 'node:assert';
import { EOL } from 'node:os';

test('correctly reports errors when an ESM module is required with --no-experimental-require-module', () => {
// The following regex matches the error message that is expected to be thrown
Expand All @@ -11,14 +12,17 @@ test('correctly reports errors when an ESM module is required with --no-experime
// const app = require('./app');
// ^

const matchRegex = /package-type-module[\\/]+require-esm-error-annotation[\\/]+index\.cjs:1[\r?\n]const app = require\('\.\/app'\);[\r?\n]\s{12}\^/;
const fixture = fixtures.path('es-modules/package-type-module/require-esm-error-annotation/index.cjs');
const args = ['--no-experimental-require-module', fixture];

const lineNumber = 1;
const lineContent = `const app = require('./app');`;
const fullMessage = `${fixture}:${lineNumber}${EOL}${lineContent}${EOL} ^${EOL}`;

const result = spawnSync(process.execPath, args);

assert.strictEqual(result.status, 1);
assert(result.stderr.toString().match(matchRegex));
assert(result.stderr.toString().indexOf(fullMessage) > -1);
assert.strictEqual(result.stdout.toString(), '');
});

Expand All @@ -29,13 +33,16 @@ test('correctly reports error for a longer stack trace', () => {
// require('./app.js')
// ^

const matchRegex = /package-type-module[\\/]+require-esm-error-annotation[\\/]+longer-stack\.cjs:6[\r?\n]\s{2}require\('\.\/app\.js'\)[\r?\n]\s{2}\^/;
const fixture = fixtures.path('es-modules/package-type-module/require-esm-error-annotation/longer-stack.cjs');
const args = ['--no-experimental-require-module', fixture];

const lineNumber = 6;
const lineContent = "require('./app.js')";
const fullMessage = `${fixture}:${lineNumber}${EOL} ${lineContent}${EOL} ^${EOL}`;

const result = spawnSync(process.execPath, args);

assert.strictEqual(result.status, 1);
assert(result.stderr.toString().match(matchRegex));
assert.strictEqual(result.stdout.toString(), '');
assert(result.stderr.toString().indexOf(fullMessage) > -1);
});

0 comments on commit e0ccecc

Please sign in to comment.