Skip to content

Commit

Permalink
prettify and add support for export default () {}
Browse files Browse the repository at this point in the history
  • Loading branch information
rauchg committed Dec 3, 2018
1 parent 7c95698 commit 18e10d1
Showing 1 changed file with 42 additions and 35 deletions.
77 changes: 42 additions & 35 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ const { dirname } = require("path");

const sourceMapSources = {};
if (!global.coverage) {
require('source-map-support').install({
retrieveSourceMap (source) {
if (!sourceMapSources[source])
return null;
require("source-map-support").install({
retrieveSourceMap(source) {
if (!sourceMapSources[source]) return null;

return {
url: source,
Expand All @@ -21,49 +20,54 @@ if (!global.coverage) {

for (const unitTest of fs.readdirSync(`${__dirname}/unit`)) {
it(`should generate correct output for ${unitTest}`, async () => {
const expected = fs.readFileSync(`${__dirname}/unit/${unitTest}/output.js`)
.toString().trim()
// Windows support
.replace(/\r/g, '');
await ncc(`${__dirname}/unit/${unitTest}/input.js`, { minify: false }).then(async ({ code, assets }) => {
// very simple asset validation in unit tests
if (unitTest.startsWith('asset-')) {
expect(Object.keys(assets).length).toBeGreaterThan(0);
expect(assets[Object.keys(assets)[0]] instanceof Buffer);
}
const actual = code.trim()
const expected = fs
.readFileSync(`${__dirname}/unit/${unitTest}/output.js`)
.toString()
.trim()
// Windows support
.replace(/\r/g, '');
try {
expect(actual).toBe(expected);
}
catch (e) {
// useful for updating fixtures
fs.writeFileSync(`${__dirname}/unit/${unitTest}/actual.js`, actual);
throw e;
.replace(/\r/g, "");
await ncc(`${__dirname}/unit/${unitTest}/input.js`, { minify: false }).then(
async ({ code, assets }) => {
// very simple asset validation in unit tests
if (unitTest.startsWith("asset-")) {
expect(Object.keys(assets).length).toBeGreaterThan(0);
expect(assets[Object.keys(assets)[0]] instanceof Buffer);
}
const actual = code
.trim()
// Windows support
.replace(/\r/g, "");
try {
expect(actual).toBe(expected);
} catch (e) {
// useful for updating fixtures
fs.writeFileSync(`${__dirname}/unit/${unitTest}/actual.js`, actual);
throw e;
}
}
});
);
});
}

// the twilio test can take a while (large codebase)
jest.setTimeout(30000);

function clearTmp () {
function clearTmp() {
try {
rimraf.sync(__dirname + "/tmp");
}
catch (e) {
if (e.code !== "ENOENT")
throw e;
} catch (e) {
if (e.code !== "ENOENT") throw e;
}
}

for (const integrationTest of fs.readdirSync(__dirname + "/integration")) {
// ignore e.g.: `.json` files
if (!integrationTest.endsWith(".js")) continue;
if (!/\.(mjs|tsx?|js)$/.test(integrationTest)) continue;
it(`should evaluate ${integrationTest} without errors`, async () => {
const { code, map, assets } = await ncc(__dirname + "/integration/" + integrationTest, { sourceMap: true });
const { code, map, assets } = await ncc(
__dirname + "/integration/" + integrationTest,
{ sourceMap: true }
);
module.exports = null;
sourceMapSources[integrationTest] = map;
// integration tests will load assets relative to __dirname
Expand All @@ -76,20 +80,23 @@ for (const integrationTest of fs.readdirSync(__dirname + "/integration")) {
(__dirname => {
try {
eval(`${code}\n//# sourceURL=${integrationTest}`);
}
catch (e) {
} catch (e) {
// useful for debugging
mkdirp.sync(__dirname);
fs.writeFileSync(__dirname + "/index.js", code);
throw e;
}
})(__dirname + "/tmp");
if ("function" !== typeof module.exports) {
const exp =
module.exports && module.exports.default
? module.exports.default
: module.exports;
if ("function" !== typeof exp) {
throw new Error(
`Integration test "${integrationTest}" evaluation failed. It does not export a function`
);
}
await module.exports();
await exp();
});
}

Expand Down

0 comments on commit 18e10d1

Please sign in to comment.