Skip to content

Commit

Permalink
Make tests actually throw when there is an error in the code and skip…
Browse files Browse the repository at this point in the history
… broken test
  • Loading branch information
lukastaegert committed Oct 20, 2020
1 parent 310b464 commit 375ba48
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 203 deletions.
2 changes: 1 addition & 1 deletion packages/commonjs/src/transform.js
Expand Up @@ -68,7 +68,7 @@ function isTrueNode(node) {

switch (node.type) {
case 'Literal':
return node.value;
return !!node.value;
case 'UnaryExpression':
return node.operator === '!' && node.argument && node.argument.value === 0;
default:
Expand Down
@@ -1,6 +1,9 @@
const { nodeResolve } = require('@rollup/plugin-node-resolve');

module.exports = {
// TODO This test is broken because for dynamic require targets with dependencies, the dependencies are hoisted
// above the dynamic register calls at the moment
skip: true,
description: 'resolves imports of node_modules module with halfway / subfolder access',
options: {
plugins: [nodeResolve()]
Expand Down
Expand Up @@ -11,4 +11,7 @@ t.is(takeModule('.//'), 'same-directory');
t.is(takeModule('./sub'), 'sub');

t.is(takeModule('custom-module'), 'custom-module + sub');
t.deepEqual(require('./sub/sub'), { parent: 'same-directory', customModule: 'custom-module' });
t.deepEqual(require('./sub/sub'), {
parent: 'same-directory',
customModule: 'custom-module + sub'
});
Expand Up @@ -4,6 +4,11 @@ module.exports = {
description:
'Creates correct exports if an ES module that was transpiled to CJS is used as entry point',
options: {
onwarn(warning) {
if (warning.code !== 'MIXED_EXPORTS') {
throw new Error(warning.message);
}
},
input: [path.join(__dirname, 'main.js'), path.join(__dirname, 'entry.js')]
}
};
Expand Up @@ -4,6 +4,11 @@ module.exports = {
description:
'Creates correct exports if an ES module that was transpiled to CJS is used as entry point',
options: {
onwarn(warning) {
if (warning.code !== 'MIXED_EXPORTS') {
throw new Error(warning.message);
}
},
input: [path.join(__dirname, 'main.js'), path.join(__dirname, 'entry.js')]
}
};
@@ -0,0 +1,3 @@
module.exports = {
description: 'handles a transpiled module that imports the default from a transpiled module'
};
@@ -0,0 +1,2 @@
Object.defineProperty(exports, '__esModule', { value: true });
exports.default = 'default';
@@ -0,0 +1,9 @@
const dep = require('./dep.js');

function _interopDefault(e) {
return e && e.__esModule ? e : { default: e };
}

t.is(dep.__esModule, true);
const dep__default = /* #__PURE__*/ _interopDefault(dep);
t.is(dep__default.default, 'default');
9 changes: 8 additions & 1 deletion packages/commonjs/test/function.js
Expand Up @@ -18,6 +18,10 @@ readdirSync('./fixtures/function').forEach((dir) => {
config = {};
}

if (config.skip) {
console.error(`Skipped test "${dir}"`);
return;
}
(config.solo ? test.only : test)(dir, async (t) => {
const options = Object.assign(
{
Expand All @@ -44,10 +48,13 @@ readdirSync('./fixtures/function').forEach((dir) => {
console.groupEnd();
}
}
const { exports, global } = runCodeSplitTest(codeMap, t, config.context);
const { exports, global, error } = runCodeSplitTest(codeMap, t, config.context);

if (config.exports) config.exports(exports, t);
if (config.global) config.global(global, t);
if (error) {
throw error;
}
t.snapshot(codeMap);
});
});
247 changes: 47 additions & 200 deletions packages/commonjs/test/snapshots/function.js.md
Expand Up @@ -2752,206 +2752,6 @@ Generated by [AVA](https://avajs.dev).
`,
}

## dynamic-require-slash-access

> Snapshot 1
{
'main.js': `'use strict';␊
␊
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊
␊
function commonjsRegister (path, loader) {␊
DYNAMIC_REQUIRE_LOADERS[path] = loader;␊
}␊
␊
const DYNAMIC_REQUIRE_LOADERS = Object.create(null);␊
const DYNAMIC_REQUIRE_CACHE = Object.create(null);␊
const DEFAULT_PARENT_MODULE = {␊
id: '<' + 'rollup>', exports: {}, parent: undefined, filename: null, loaded: false, children: [], paths: []␊
};␊
const CHECKED_EXTENSIONS = ['', '.js', '.json'];␊
␊
function normalize (path) {␊
path = path.replace(/\\\\/g, '/');␊
const parts = path.split('/');␊
const slashed = parts[0] === '';␊
for (let i = 1; i < parts.length; i++) {␊
if (parts[i] === '.' || parts[i] === '') {␊
parts.splice(i--, 1);␊
}␊
}␊
for (let i = 1; i < parts.length; i++) {␊
if (parts[i] !== '..') continue;␊
if (i > 0 && parts[i - 1] !== '..' && parts[i - 1] !== '.') {␊
parts.splice(--i, 2);␊
i--;␊
}␊
}␊
path = parts.join('/');␊
if (slashed && path[0] !== '/')␊
path = '/' + path;␊
else if (path.length === 0)␊
path = '.';␊
return path;␊
}␊
␊
function join () {␊
if (arguments.length === 0)␊
return '.';␊
let joined;␊
for (let i = 0; i < arguments.length; ++i) {␊
let arg = arguments[i];␊
if (arg.length > 0) {␊
if (joined === undefined)␊
joined = arg;␊
else␊
joined += '/' + arg;␊
}␊
}␊
if (joined === undefined)␊
return '.';␊
␊
return joined;␊
}␊
␊
function isPossibleNodeModulesPath (modulePath) {␊
let c0 = modulePath[0];␊
if (c0 === '/' || c0 === '\\\\') return false;␊
let c1 = modulePath[1], c2 = modulePath[2];␊
if ((c0 === '.' && (!c1 || c1 === '/' || c1 === '\\\\')) ||␊
(c0 === '.' && c1 === '.' && (!c2 || c2 === '/' || c2 === '\\\\'))) return false;␊
if (c1 === ':' && (c2 === '/' || c2 === '\\\\'))␊
return false;␊
return true;␊
}␊
␊
function dirname (path) {␊
if (path.length === 0)␊
return '.';␊
␊
let i = path.length - 1;␊
while (i > 0) {␊
const c = path.charCodeAt(i);␊
if ((c === 47 || c === 92) && i !== path.length - 1)␊
break;␊
i--;␊
}␊
␊
if (i > 0)␊
return path.substr(0, i);␊
␊
if (path.chartCodeAt(0) === 47 || path.chartCodeAt(0) === 92)␊
return path.charAt(0);␊
␊
return '.';␊
}␊
␊
function commonjsRequire (path, originalModuleDir) {␊
const shouldTryNodeModules = isPossibleNodeModulesPath(path);␊
path = normalize(path);␊
let relPath;␊
while (true) {␊
if (!shouldTryNodeModules) {␊
relPath = originalModuleDir ? normalize(originalModuleDir + '/' + path) : path;␊
} else if (originalModuleDir) {␊
relPath = normalize(originalModuleDir + '/node_modules/' + path);␊
} else {␊
relPath = normalize(join('node_modules', path));␊
}␊
for (let extensionIndex = 0; extensionIndex < CHECKED_EXTENSIONS.length; extensionIndex++) {␊
const resolvedPath = relPath + CHECKED_EXTENSIONS[extensionIndex];␊
let cachedModule = DYNAMIC_REQUIRE_CACHE[resolvedPath];␊
if (cachedModule) return cachedModule.exports;␊
const loader = DYNAMIC_REQUIRE_LOADERS[resolvedPath];␊
if (loader) {␊
DYNAMIC_REQUIRE_CACHE[resolvedPath] = cachedModule = {␊
id: resolvedPath,␊
filename: resolvedPath,␊
path: dirname(resolvedPath),␊
exports: {},␊
parent: DEFAULT_PARENT_MODULE,␊
loaded: false,␊
children: [],␊
paths: [],␊
require: function (path, base) {␊
return commonjsRequire(path, (base === undefined || base === null) ? cachedModule.path : base);␊
}␊
};␊
try {␊
loader.call(commonjsGlobal, cachedModule, cachedModule.exports);␊
} catch (error) {␊
delete DYNAMIC_REQUIRE_CACHE[resolvedPath];␊
throw error;␊
}␊
cachedModule.loaded = true;␊
return cachedModule.exports;␊
} }␊
if (!shouldTryNodeModules) break;␊
const nextDir = normalize(originalModuleDir + '/..');␊
if (nextDir === originalModuleDir) break;␊
originalModuleDir = nextDir;␊
}␊
return require(path);␊
}␊
␊
commonjsRequire.cache = DYNAMIC_REQUIRE_CACHE;␊
␊
var dynamicRequireSlashAccess = 'same-directory';␊
␊
var sub = 'sub';␊
␊
var customModule = 'custom-module' + ' + ' + commonjsRequire("custom-module2/sub", "/$$rollup_base$$/fixtures/function/dynamic-require-slash-access/node_modules/custom-module");␊
␊
commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-slash-access/node_modules/custom-module2/sub.js", function (module, exports) {␊
module.exports = 'sub';␊
␊
});␊
␊
commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-slash-access", function (module, exports) {␊
module.exports = dynamicRequireSlashAccess;␊
});␊
commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-slash-access/sub", function (module, exports) {␊
module.exports = sub;␊
});␊
commonjsRegister("/$$rollup_base$$/fixtures/function/dynamic-require-slash-access/node_modules/custom-module", function (module, exports) {␊
module.exports = customModule;␊
});␊
␊
/* eslint-disable import/no-dynamic-require, global-require */␊
␊
function takeModule(name) {␊
return commonjsRequire(name,"/$$rollup_base$$/fixtures/function/dynamic-require-slash-access/sub");␊
}␊
␊
var sub$1 = {␊
parent: takeModule('..'),␊
customModule: takeModule('custom-module')␊
};␊
␊
/* eslint-disable import/no-dynamic-require, global-require */␊
␊
function takeModule$1(name) {␊
return commonjsRequire(name,"/$$rollup_base$$/fixtures/function/dynamic-require-slash-access");␊
}␊
␊
t.is(takeModule$1('.'), 'same-directory');␊
t.is(takeModule$1('./'), 'same-directory');␊
t.is(takeModule$1('.//'), 'same-directory');␊
␊
t.is(takeModule$1('./sub'), 'sub');␊
␊
t.is(takeModule$1('custom-module'), 'custom-module + sub');␊
t.deepEqual(sub$1, { parent: 'same-directory', customModule: 'custom-module' });␊
␊
var main = {␊
␊
};␊
␊
module.exports = main;␊
`,
}

## es6-export-with-global-sniffing

> Snapshot 1
Expand Down Expand Up @@ -5130,6 +4930,53 @@ Generated by [AVA](https://avajs.dev).
`,
}

## transpiled-esm-importer

> Snapshot 1
{
'main.js': `'use strict';␊
␊
var _default = 'default';␊
␊
var dep = /*#__PURE__*/Object.freeze({␊
__proto__: null,␊
'default': _default␊
});␊
␊
function getAugmentedNamespace(n) {␊
if (n.__esModule) return n;␊
var a = Object.defineProperty({}, '__esModule', {value: true});␊
Object.keys(n).forEach(function (k) {␊
var d = Object.getOwnPropertyDescriptor(n, k);␊
Object.defineProperty(a, k, d.get ? d : {␊
enumerable: true,␊
get: function () {␊
return n[k];␊
}␊
});␊
});␊
return a;␊
}␊
␊
var dep$1 = /*@__PURE__*/getAugmentedNamespace(dep);␊
␊
function _interopDefault$1(e) {␊
return e && e.__esModule ? e : { default: e };␊
}␊
␊
t.is(dep$1.__esModule, true);␊
const dep__default = /* #__PURE__*/ _interopDefault$1(dep$1);␊
t.is(dep__default.default, 'default');␊
␊
var main = {␊
␊
};␊
␊
module.exports = main;␊
`,
}

## transpiled-esm-mixed

> Snapshot 1
Expand Down
Binary file modified packages/commonjs/test/snapshots/function.js.snap
Binary file not shown.

0 comments on commit 375ba48

Please sign in to comment.