|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { spawnPromisified } = require('../common'); |
| 4 | +const tmpdir = require('../common/tmpdir'); |
| 5 | +const assert = require('assert'); |
| 6 | +const { writeFileSync, unlink } = require('fs'); |
| 7 | +const { describe, after, it } = require('node:test'); |
| 8 | + |
| 9 | +tmpdir.refresh(); |
| 10 | + |
| 11 | +const fileImports = { |
| 12 | + cjs: 'const assert = require("assert");', |
| 13 | + mjs: 'import assert from "assert";', |
| 14 | +}; |
| 15 | + |
| 16 | +const fileNames = []; |
| 17 | + |
| 18 | +for (const [ext, header] of Object.entries(fileImports)) { |
| 19 | + const fileName = `test-file.${ext}`; |
| 20 | + // Store the generated filesnames in an array |
| 21 | + fileNames.push(`${tmpdir.path}/${fileName}`); |
| 22 | + |
| 23 | + writeFileSync(tmpdir.resolve(fileName), `${header}\nassert.ok(0 === 2);`); |
| 24 | +} |
| 25 | + |
| 26 | +describe('ensure the assert.ok throwing similar error messages for esm and cjs files', () => { |
| 27 | + const nodejsPath = `${process.execPath}`; |
| 28 | + const errorsMessages = []; |
| 29 | + |
| 30 | + it('should return code 1 for each command', async () => { |
| 31 | + for (const fileName of fileNames) { |
| 32 | + const { stderr, code } = await spawnPromisified(nodejsPath, [fileName]); |
| 33 | + assert.strictEqual(code, 1); |
| 34 | + // For each error message, filter the lines which will starts with AssertionError |
| 35 | + errorsMessages.push( |
| 36 | + stderr.split('\n').find((s) => s.startsWith('AssertionError')) |
| 37 | + ); |
| 38 | + } |
| 39 | + }); |
| 40 | + |
| 41 | + after(() => { |
| 42 | + assert.strictEqual(errorsMessages.length, 2); |
| 43 | + assert.deepStrictEqual(errorsMessages[0], errorsMessages[1]); |
| 44 | + |
| 45 | + for (const fileName of fileNames) { |
| 46 | + unlink(fileName, () => {}); |
| 47 | + } |
| 48 | + |
| 49 | + tmpdir.refresh(); |
| 50 | + }); |
| 51 | +}); |
0 commit comments