test: add UMD module test with marked

PR-URL: https://github.com/nodejs/node/pull/38905
Refs: https://github.com/nodejs/node/issues/35711
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
Joyee Cheung 2021-09-10 00:46:40 +08:00
parent 3611045dfc
commit a63af1fd64
No known key found for this signature in database
GPG Key ID: 92B78A53C8303B8D
3 changed files with 3068 additions and 0 deletions

21
test/fixtures/snapshot/check-marked.js vendored Normal file
View File

@ -0,0 +1,21 @@
'use strict';
let marked;
if (process.env.NODE_TEST_USE_SNAPSHOT === 'true') {
console.error('NODE_TEST_USE_SNAPSHOT true');
marked = globalThis.marked;
} else {
console.error('NODE_TEST_USE_SNAPSHOT false');
marked = require('./marked');
}
const md = `
# heading
[link][1]
[1]: #heading "heading"
`;
const html = marked(md)
console.log(html);

2970
test/fixtures/snapshot/marked.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,77 @@
'use strict';
// This tests the behavior of loading a UMD module with --build-snapshot
require('../common');
const assert = require('assert');
const { spawnSync } = require('child_process');
const tmpdir = require('../common/tmpdir');
const fixtures = require('../common/fixtures');
const path = require('path');
const fs = require('fs');
tmpdir.refresh();
const blobPath = path.join(tmpdir.path, 'snapshot.blob');
const file = fixtures.path('snapshot', 'marked.js');
{
// By default, the snapshot blob path is snapshot.blob at cwd
const child = spawnSync(process.execPath, [
'--snapshot-blob',
blobPath,
'--build-snapshot',
file,
], {
cwd: tmpdir.path
});
const stderr = child.stderr.toString();
const stdout = child.stdout.toString();
console.log(stderr);
console.log(stdout);
assert.strictEqual(child.status, 0);
const stats = fs.statSync(path.join(tmpdir.path, 'snapshot.blob'));
assert(stats.isFile());
}
{
let child = spawnSync(process.execPath, [
'--snapshot-blob',
path.join(tmpdir.path, 'snapshot.blob'),
fixtures.path('snapshot', 'check-marked.js'),
], {
cwd: tmpdir.path,
env: {
...process.env,
NODE_TEST_USE_SNAPSHOT: 'true'
}
});
let stderr = child.stderr.toString();
const snapshotOutput = child.stdout.toString();
console.log(stderr);
console.log(snapshotOutput);
assert.strictEqual(child.status, 0);
assert(stderr.includes('NODE_TEST_USE_SNAPSHOT true'));
child = spawnSync(process.execPath, [
'--snapshot-blob',
blobPath,
fixtures.path('snapshot', 'check-marked.js'),
], {
cwd: tmpdir.path,
env: {
...process.env,
NODE_TEST_USE_SNAPSHOT: 'false'
}
});
stderr = child.stderr.toString();
const verifyOutput = child.stdout.toString();
console.log(stderr);
console.log(verifyOutput);
assert.strictEqual(child.status, 0);
assert(stderr.includes('NODE_TEST_USE_SNAPSHOT false'));
assert(snapshotOutput.includes(verifyOutput));
}