From 3111b9de241b3700e127306e71a6627e438afbee Mon Sep 17 00:00:00 2001 From: Corey Farrell Date: Mon, 11 Mar 2019 20:23:33 -0400 Subject: [PATCH] feat: Create @istanbul/nyc-config-hook-run-in-this-context. (#332) This base configuration helps enable `hook-run-in-this-context` in a way that instruments files loaded via `require()` only once regardless of node.js version. --- .../LICENSE | 6 ++++ .../README.md | 30 ++++++++++++++++ .../index.js | 8 +++++ .../package.json | 35 +++++++++++++++++++ .../test/index.test.js | 12 +++++++ 5 files changed, 91 insertions(+) create mode 100644 packages/nyc-config-hook-run-in-this-context/LICENSE create mode 100644 packages/nyc-config-hook-run-in-this-context/README.md create mode 100644 packages/nyc-config-hook-run-in-this-context/index.js create mode 100644 packages/nyc-config-hook-run-in-this-context/package.json create mode 100644 packages/nyc-config-hook-run-in-this-context/test/index.test.js diff --git a/packages/nyc-config-hook-run-in-this-context/LICENSE b/packages/nyc-config-hook-run-in-this-context/LICENSE new file mode 100644 index 00000000..50b947a9 --- /dev/null +++ b/packages/nyc-config-hook-run-in-this-context/LICENSE @@ -0,0 +1,6 @@ +Copyright 2019 Contributors + +Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + diff --git a/packages/nyc-config-hook-run-in-this-context/README.md b/packages/nyc-config-hook-run-in-this-context/README.md new file mode 100644 index 00000000..ea07a474 --- /dev/null +++ b/packages/nyc-config-hook-run-in-this-context/README.md @@ -0,0 +1,30 @@ +# nyc-config-hook-run-in-this-context + +Handy configuration for instrumenting with hook-run-in-this-context enabled. + +Prior to node.js 11.11.0 `require()` was implemented using `vm.runInThisContext()`. +This meant that running with `hook-run-in-this-context` enabled required disabling +`hook-require`. Starting with node 11.11.0 `require()` is no longer implemented +with `vm.runInThisContext()`, so `hook-require` still needs to be enabled. This +base configuration enables `hook-run-in-this-context` and provides the correct +setting for `hook-require` to ensure that modules loaded by `require()` are +instrumented once. + +First install the dependencies: + +`npm i nyc @istanbuljs/nyc-config-hook-run-in-this-context --save-dev` + +## .nycrc + +And write a `.nycrc` that looks like this: + +```json +{ + "extends": "@istanbuljs/nyc-config-hook-run-in-this-context", + /* add custom settings */ +} +``` + +## License + +ISC diff --git a/packages/nyc-config-hook-run-in-this-context/index.js b/packages/nyc-config-hook-run-in-this-context/index.js new file mode 100644 index 00000000..de15a944 --- /dev/null +++ b/packages/nyc-config-hook-run-in-this-context/index.js @@ -0,0 +1,8 @@ +'use strict'; + +const semver = require('semver'); + +module.exports = { + 'hook-require': !semver.lt(process.version, '11.11.0'), + 'hook-run-in-this-context': true +}; diff --git a/packages/nyc-config-hook-run-in-this-context/package.json b/packages/nyc-config-hook-run-in-this-context/package.json new file mode 100644 index 00000000..48d8d918 --- /dev/null +++ b/packages/nyc-config-hook-run-in-this-context/package.json @@ -0,0 +1,35 @@ +{ + "name": "@istanbuljs/nyc-config-hook-run-in-this-context", + "version": "0.1.0", + "description": "nyc configuration for hook-run-in-this-context", + "main": "index.js", + "files": [ + "index.js" + ], + "scripts": { + "test": "mocha" + }, + "repository": { + "type": "git", + "url": "git+ssh://git@github.com/istanbuljs/istanbuljs.git" + }, + "keywords": [ + "hook", + "config", + "nyc", + "test", + "coverage" + ], + "author": "Corey Farrell ", + "license": "ISC", + "bugs": { + "url": "https://github.com/istanbuljs/istanbuljs/issues" + }, + "homepage": "https://istanbul.js.org/", + "publishConfig": { + "access": "public" + }, + "engines": { + "node": ">=6" + } +} diff --git a/packages/nyc-config-hook-run-in-this-context/test/index.test.js b/packages/nyc-config-hook-run-in-this-context/test/index.test.js new file mode 100644 index 00000000..8982c766 --- /dev/null +++ b/packages/nyc-config-hook-run-in-this-context/test/index.test.js @@ -0,0 +1,12 @@ +/* global describe, it */ + +const assert = require('chai').assert; +const index = require('../index'); + +describe('external interface', () => { + it('exports the correct interface', () => { + assert.equal(typeof index, 'object'); + assert.ok('hook-require' in index); + assert.equal(index['hook-run-in-this-context'], true); + }); +});