From cda9378dad338853ac2c864bbb433d59f912a53a Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Wed, 5 Feb 2020 09:50:11 -0500 Subject: [PATCH 1/2] Add a few more e2e tests (#34) * fix: restore default export * another e2e test * test default import * do not snapshot mul e2e test value --- __snapshots__/e2e_spec.js | 30 ++++++++++++++++++++++++++++++ test/e2e/e2e_spec.js | 20 +++++++++++++++++++- test/fixtures/mul.js | 1 + test/fixtures/mul_spec.js | 10 ++++++++++ test/fixtures/sub.js | 3 +++ test/fixtures/sub_spec.js | 10 ++++++++++ 6 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/mul.js create mode 100644 test/fixtures/mul_spec.js create mode 100644 test/fixtures/sub.js create mode 100644 test/fixtures/sub_spec.js diff --git a/__snapshots__/e2e_spec.js b/__snapshots__/e2e_spec.js index 3ee91ac..da1124a 100644 --- a/__snapshots__/e2e_spec.js +++ b/__snapshots__/e2e_spec.js @@ -48,3 +48,33 @@ context('math.js', function () { },{"./math":1}]},{},[2]); ` + +exports['sub import'] = ` +(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i { it('handles imports and exports', () => { - return bundle('math_spec.js').then((output) => { // check that bundled tests work eval(output) snapshot('math default exports', output) }) }) + + it('handles module.exports and import', () => { + return bundle('sub_spec.js').then((output) => { + // check that bundled tests work + eval(output) + snapshot('sub import', output) + }) + }) + + it('handles module.exports and default import', () => { + return bundle('mul_spec.js').then((output) => { + // check that bundled tests work + eval(output) + // for some reason, this bundle included full resolved path + // to interop require module + // which on CI generates different path. + // so as long as eval works, do not snapshot it + }) + }) }) diff --git a/test/fixtures/mul.js b/test/fixtures/mul.js new file mode 100644 index 0000000..365d802 --- /dev/null +++ b/test/fixtures/mul.js @@ -0,0 +1 @@ +module.exports = (a, b) => a * b diff --git a/test/fixtures/mul_spec.js b/test/fixtures/mul_spec.js new file mode 100644 index 0000000..a6b3f49 --- /dev/null +++ b/test/fixtures/mul_spec.js @@ -0,0 +1,10 @@ +import mul from './mul' + +context('mul.js imports default', function () { + it('imports function', () => { + expect(mul, 'mul').to.be.a('function') + }) + it('can multiply numbers', function () { + expect(mul(3, 2)).to.eq(6) + }) +}) diff --git a/test/fixtures/sub.js b/test/fixtures/sub.js new file mode 100644 index 0000000..5ac0f50 --- /dev/null +++ b/test/fixtures/sub.js @@ -0,0 +1,3 @@ +const sub = (a, b) => a - b + +module.exports = {sub} diff --git a/test/fixtures/sub_spec.js b/test/fixtures/sub_spec.js new file mode 100644 index 0000000..a480363 --- /dev/null +++ b/test/fixtures/sub_spec.js @@ -0,0 +1,10 @@ +import { sub } from './sub' + +context('sub.js', function () { + it('imports function', () => { + expect(sub, 'sub').to.be.a('function') + }) + it('can subtract numbers', function () { + expect(sub(1, 2)).to.eq(-1) + }) +}) From 8601521fb728169278f0ab3c4020a19fff893948 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Wed, 5 Feb 2020 11:09:38 -0500 Subject: [PATCH 2/2] fix: use newer module exports plugin (#35) * export string test * fix: follow ES6 when exporting default * remove commented out code --- __snapshots__/e2e_spec.js | 35 --------- index.js | 6 +- package-lock.json | 147 ++++++++++++++++++++++++++++++++--- package.json | 2 +- test/e2e/e2e_spec.js | 16 +++- test/fixtures/divide.js | 2 + test/fixtures/divide_spec.js | 8 ++ test/fixtures/dom.js | 1 + test/fixtures/dom_spec.js | 8 ++ test/fixtures/math_spec.js | 5 +- 10 files changed, 175 insertions(+), 55 deletions(-) create mode 100644 test/fixtures/divide.js create mode 100644 test/fixtures/divide_spec.js create mode 100644 test/fixtures/dom.js create mode 100644 test/fixtures/dom_spec.js diff --git a/__snapshots__/e2e_spec.js b/__snapshots__/e2e_spec.js index da1124a..863ef64 100644 --- a/__snapshots__/e2e_spec.js +++ b/__snapshots__/e2e_spec.js @@ -14,41 +14,6 @@ it('is a test', function () { ` -exports['math default exports'] = ` -(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i { return bundle('math_spec.js').then((output) => { // check that bundled tests work eval(output) - snapshot('math default exports', output) }) }) + it('named ES6', () => { + return bundle('divide_spec.js').then((output) => { + // check that bundled tests work + eval(output) + }) + }) + + it('handles module.exports and import', () => { return bundle('sub_spec.js').then((output) => { // check that bundled tests work @@ -60,4 +67,11 @@ describe('imports and exports', () => { // so as long as eval works, do not snapshot it }) }) + + it('handles default string import', () => { + return bundle('dom_spec.js').then((output) => { + // check that bundled tests work + eval(output) + }) + }) }) diff --git a/test/fixtures/divide.js b/test/fixtures/divide.js new file mode 100644 index 0000000..c6b2e1b --- /dev/null +++ b/test/fixtures/divide.js @@ -0,0 +1,2 @@ +// named export +export const divide = (a, b) => a/b diff --git a/test/fixtures/divide_spec.js b/test/fixtures/divide_spec.js new file mode 100644 index 0000000..fe24d1b --- /dev/null +++ b/test/fixtures/divide_spec.js @@ -0,0 +1,8 @@ +import { divide } from './divide' + +context('ES6 named export and import', function () { + it('works', () => { + expect(divide, 'divide').to.be.a('function') + expect(divide(10, 2)).to.eq(5) + }) +}) diff --git a/test/fixtures/dom.js b/test/fixtures/dom.js new file mode 100644 index 0000000..74486ae --- /dev/null +++ b/test/fixtures/dom.js @@ -0,0 +1 @@ +export default 'dom' diff --git a/test/fixtures/dom_spec.js b/test/fixtures/dom_spec.js new file mode 100644 index 0000000..f7bb120 --- /dev/null +++ b/test/fixtures/dom_spec.js @@ -0,0 +1,8 @@ +const dom = require('./dom').default + +context('imports default string', function () { + it('works', () => { + expect(dom, 'dom').to.be.a('string') + expect(dom).to.equal('dom') + }) +}) diff --git a/test/fixtures/math_spec.js b/test/fixtures/math_spec.js index 1443cf3..a48b68d 100644 --- a/test/fixtures/math_spec.js +++ b/test/fixtures/math_spec.js @@ -1,4 +1,7 @@ -import { add } from './math' +// math exports default object +// so if we want a property, first we need to grab the default +import math from './math' +const {add} = math context('math.js', function () { it('imports function', () => {