webpack/test/helpers/expectSource.js

29 lines
1.1 KiB
JavaScript
Raw Normal View History

"use strict";
const regexEscape = require("./regexEscape");
// These expect* methods are necessary because 'source' contains the code for this test file, which will always contain the string
// being tested for, so we have to use the "DO NOT MATCH BELOW..." technique to exclude the actual testing code from the test.
// Place your jest 'expect' calls below a line containing the DO NOT MATCH BELOW... string constructed below. See other tests for examples.
2023-05-10 06:13:20 +08:00
// Break up the match string so we don't match it in these expect* functions either.
const doNotMatch = ["DO", "NOT", "MATCH", "BELOW", "THIS", "LINE"].join(" ");
2025-04-22 20:42:33 +08:00
/**
* @param {string} source value
* @param {string} str string for searching
*/
function expectSourceToContain(source, str) {
2024-07-31 10:39:30 +08:00
expect(source).toMatch(new RegExp(`${regexEscape(str)}.*${doNotMatch}`, "s"));
}
2025-04-22 20:42:33 +08:00
/**
* @param {string} source value
* @param {RegExp} regexStr regexp
*/
function expectSourceToMatch(source, regexStr) {
2024-07-31 10:39:30 +08:00
expect(source).toMatch(new RegExp(`${regexStr}.*${doNotMatch}`, "s"));
}
module.exports = { expectSourceToContain, expectSourceToMatch };