Skip to content

Commit

Permalink
feat: Create @istanbul/nyc-config-hook-run-in-this-context. (#332)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
coreyfarrell committed Mar 12, 2019
1 parent 7f90f57 commit 3111b9d
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
6 changes: 6 additions & 0 deletions 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.

30 changes: 30 additions & 0 deletions 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
8 changes: 8 additions & 0 deletions 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
};
35 changes: 35 additions & 0 deletions 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 <git@cfware.com>",
"license": "ISC",
"bugs": {
"url": "https://github.com/istanbuljs/istanbuljs/issues"
},
"homepage": "https://istanbul.js.org/",
"publishConfig": {
"access": "public"
},
"engines": {
"node": ">=6"
}
}
12 changes: 12 additions & 0 deletions 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);
});
});

0 comments on commit 3111b9d

Please sign in to comment.