Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
esm support done
  • Loading branch information
giltayar authored and searls committed May 16, 2020
1 parent 602ed0e commit 50f7d99
Show file tree
Hide file tree
Showing 21 changed files with 280 additions and 47 deletions.
22 changes: 22 additions & 0 deletions index.d.ts
Expand Up @@ -261,6 +261,28 @@ export function callback(...args: any[]): void;
* @returns {*}
*/
export function replace(path: string, f?: any): any;
/**
* Swap out real dependenencies with fake one. Intercept calls to `require`
* that dependency module and ensure your subject is handed a fake instead.
*
* @export
* @param {string} path
* @param {*} [f]
* @returns {*}
*/
export function replaceCjs(path: string, f?: any): any;
/**
* Swap out real dependenencies with fake one. Intercept calls to `import`
* that dependency module and ensure your subject is handed a fake instead.
*
* @export
* @param {string} path
* @param {*} [namedExportStubs]
* @param {*} [defaultExportStub]
* @returns {Promise<{default?: any, [namedExport: string]: any}>}
*/
export function replaceEsm(path: string, namedExportStubs?: any, defaultExportStub?: any):
Promise<{default?: any, [namedExport: string]: any}>;

/**
* Swap out real dependenencies with fake one. Reference to the property will
Expand Down
2 changes: 2 additions & 0 deletions notypescript/import-esm-car.js
@@ -0,0 +1,2 @@
// @ts-ignore
module.exports = () => import('../test/safe-esm/fixtures/car.mjs')
95 changes: 64 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 10 additions & 6 deletions package.json
Expand Up @@ -22,7 +22,7 @@
"clean": "rimraf dist lib coverage",
"clean:hard": "npm run clean && rimraf node_modules \"examples/*/node_modules\"",
"postclean": "mkdirp dist",
"compile:browser": "cross-conf-env browserify src/index.js --standalone td --outfile $npm_package_config_build_file -p tsify -p headerify",
"compile:browser": "cross-conf-env browserify src/index.js --standalone td --outfile $npm_package_config_build_file -p [ tsify --project tsconfig-browser.json ] -p headerify",
"compile:node": "tsc",
"precompile": "npm run clean",
"compile": "run-s compile:node compile:browser",
Expand All @@ -32,10 +32,11 @@
"style": "run-p style:js style:ts",
"style:js": "standard --fix",
"style:ts": "standard --fix --parser @typescript-eslint/parser --plugin @typescript-eslint/eslint-plugin \"**/*.ts\"",
"test": "teenytest --helper test/helper.js \"test/**/*.test.{js,ts}\"",
"test": "NODE_OPTIONS='--loader=quibble' TS_NODE_IGNORE='(?:^|/)node_modules/,notypescript' ./test/safe-esm/teenytest-proxy.js --helper test/helper.js \"test/**/*.test.{js,ts}\"",
"test:all": "run-s test test:example",
"test:unit": "teenytest --helper test/helper.js \"test/unit/**/*.test.{js,ts}\"",
"test:safe": "teenytest --helper test/helper.js \"test/safe/**/*.test.{js,ts}\"",
"test:esm": "NODE_OPTIONS='--loader=quibble' TS_NODE_IGNORE='(?:^|/)node_modules/,notypescript' ./test/safe-esm/teenytest-proxy.js --helper test/helper.js test/safe-esm/replace.test.js",
"test:ci": "npm run compile && run-p style test:all && echo \"All done!\"",
"test:example": "run-p test:example:babel test:example:jest test:example:jest-broken test:example:plain-html test:example:node test:example:node-ava test:example:webpack",
"test:example:babel": "./script/run-examples babel",
Expand Down Expand Up @@ -78,12 +79,13 @@
},
"teenytest": {
"plugins": [
"test/support/tdify-plugin.js"
"test/support/tdify-plugin.js",
"teenytest-promise"
]
},
"dependencies": {
"lodash": "^4.17.15",
"quibble": "^0.5.7",
"quibble": "^0.6.0",
"stringify-object-es5": "^2.5.0",
"theredoc": "^1.0.0"
},
Expand All @@ -97,14 +99,16 @@
"dedent": "^0.7.0",
"headerify": "^1.0.1",
"is-number": "^7.0.0",
"is-promise": "^4.0.0",
"mkdirp": "^1.0.3",
"npm-run-all": "^4.1.5",
"nyc": "^15.0.0",
"rimraf": "^3.0.2",
"standard": "^14.3.1",
"teenytest": "^5.3.0",
"teenytest": "^6.0.0",
"teenytest-promise": "^1.0.0",
"testdouble": "^3.12.5",
"ts-node": "^8.6.2",
"ts-node": "^8.10.1",
"tsify": "^4.0.1",
"typescript": "^3.8.2"
},
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Expand Up @@ -5,7 +5,7 @@ import imitate from './imitate'
import when from './when'
import verify from './verify'
import matchers from './matchers'
import replace from './replace'
import replace, { replaceEsm } from './replace'
import explain from './explain'
import reset from './reset'
import config from './config'
Expand All @@ -23,6 +23,7 @@ module.exports = {
verify,
matchers,
replace,
replaceEsm,
explain,
reset,
config,
Expand Down
8 changes: 7 additions & 1 deletion src/replace/index.js
@@ -1,6 +1,6 @@
import _ from '../wrap/lodash'
import * as quibble from 'quibble'
import replaceModule from './module'
import replaceModule, { replaceEsModule } from './module'
import replaceProperty from './property'

quibble.ignoreCallsFromThisFile()
Expand All @@ -12,3 +12,9 @@ export default function (target) {
return replaceProperty(...arguments)
}
}

export function replaceEsm (_modulePath, _namedExportReplacement, _defaultExportReplacement) {
// Sending arguments instead of the above arguments is crucial because `replaceEsModule`
// uses arguments.length to figure out what to do.
return replaceEsModule(...arguments)
}
19 changes: 18 additions & 1 deletion src/replace/module/index.js
Expand Up @@ -7,11 +7,28 @@ import fakeName from './fake-name'

quibble.ignoreCallsFromThisFile()

export default function replaceModule (path, stub) {
export default function replaceCjsModule (path, stub) {
if (typeof jest === 'object') return jestModule(...arguments)
if (arguments.length > 1) { return quibble(path, stub) }
const realThing = requireActual(path)
const fakeThing = imitate(realThing, fakeName(path, realThing))
quibble(path, fakeThing)
return fakeThing
}

export async function replaceEsModule (path, namedExportsStub, defaultExportStub) {
if (typeof jest === 'object') {
throw new Error(`stubbing ES modules (${path}) under Jest is not yet supported`)
}
if (arguments.length > 1) {
return quibble.esm(path, namedExportsStub, defaultExportStub)
}

const { modulePath, module } = await quibble.esmImportWithPath(path)
const { default: fakeDefaultExport = undefined, ...fakeNamedExports } =
imitate(module, fakeName(path, module))

await quibble.esm(modulePath, fakeNamedExports, fakeDefaultExport)

return { default: fakeDefaultExport, ...fakeNamedExports }
}
22 changes: 22 additions & 0 deletions test/safe-esm/fixtures/car.mjs
@@ -0,0 +1,22 @@
import Passenger from './passenger.mjs'
import isPromise from 'is-promise'
import honk from './honk.mjs'
import turn from './turn.mjs'
import brake from './brake.mjs'
import * as lights from './lights.mjs'
import shift, { neutral } from './shift.mjs'

export default {
seatPassenger: function () {
return new Passenger().sit()
},
honk,
turn,
brake,
lights,
shift,
neutral,
isASpeed: function (thing) {
return isPromise(thing)
}
}
7 changes: 7 additions & 0 deletions test/safe-esm/fixtures/es6class.mjs
@@ -0,0 +1,7 @@
class ES6Class {
foo () { return 'og foo' }

bar () { return 'og bar' }
}

export default ES6Class
3 changes: 3 additions & 0 deletions test/safe-esm/fixtures/honk.mjs
@@ -0,0 +1,3 @@
export default function () {
throw 'honk' // eslint-disable-line
}
13 changes: 13 additions & 0 deletions test/safe-esm/fixtures/lights.mjs
@@ -0,0 +1,13 @@
export const count = 4
export function headlight () {
throw 'headlight' // eslint-disable-line
}
export function turnSignal () {
throw 'turnSignal' // eslint-disable-line
}

export class Brights {
beBright () {
throw 'too bright!' // eslint-disable-line
}
}
5 changes: 5 additions & 0 deletions test/safe-esm/fixtures/passenger.mjs
@@ -0,0 +1,5 @@
export default class Passenger {
sit () {
throw 'i am sitting' // eslint-disable-line
}
}
7 changes: 7 additions & 0 deletions test/safe-esm/fixtures/shift.mjs
@@ -0,0 +1,7 @@
export default function shift () {
return 'Faster'
}

export function neutral () {
return 'Coast'
}
1 change: 1 addition & 0 deletions test/safe-esm/fixtures/turn.mjs
@@ -0,0 +1 @@
export default function turn () {}

0 comments on commit 50f7d99

Please sign in to comment.