-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support virtual contexts (#50)
This adds support for virtual contexts by testing `RegExp` and `Error` via `toString` instead of an `instanceof` check.
- Loading branch information
Showing
4 changed files
with
101 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { createContext, runInContext } from 'node:vm'; | ||
import { assert } from 'simple-assert'; | ||
import * as checkError from '../index.js'; | ||
|
||
const vmContext = { checkError }; | ||
createContext(vmContext); | ||
|
||
function runCodeInVm(code) { | ||
return runInContext(`{${ code }}`, vmContext); | ||
} | ||
|
||
describe('node virtual machines', function () { | ||
it('compatibleMessage', function () { | ||
assert(runCodeInVm(` | ||
const errorInstance = new Error('I am an instance'); | ||
checkError.compatibleMessage(errorInstance, /instance$/) === true; | ||
`) === true); | ||
}); | ||
|
||
it('constructorName', function () { | ||
assert(runCodeInVm(` | ||
const errorInstance = new Error('I am an instance'); | ||
checkError.getConstructorName(errorInstance); | ||
`) === 'Error'); | ||
assert(runCodeInVm(` | ||
const derivedInstance = new TypeError('I inherit from Error'); | ||
checkError.getConstructorName(derivedInstance); | ||
`) === 'TypeError'); | ||
}); | ||
|
||
it('compatibleInstance', function () { | ||
assert(runCodeInVm(` | ||
const errorInstance = new Error('I am an instance'); | ||
const sameInstance = errorInstance; | ||
checkError.compatibleInstance(errorInstance, sameInstance); | ||
`) === true); | ||
assert(runCodeInVm(` | ||
const errorInstance = new Error('I am an instance'); | ||
const otherInstance = new Error('I am another'); | ||
checkError.compatibleInstance(errorInstance, otherInstance); | ||
`) === false); | ||
}); | ||
|
||
it('compatibleConstructor', function () { | ||
assert(runCodeInVm(` | ||
const errorInstance = new Error('I am an instance'); | ||
const sameInstance = errorInstance; | ||
checkError.compatibleConstructor(errorInstance, sameInstance); | ||
`) === true); | ||
assert(runCodeInVm(` | ||
const errorInstance = new Error('I am an instance'); | ||
const otherInstance = new Error('I an another instance'); | ||
checkError.compatibleConstructor(errorInstance, otherInstance); | ||
`) === true); | ||
assert(runCodeInVm(` | ||
const errorInstance = new Error('I am an instance'); | ||
const derivedInstance = new TypeError('I inherit from Error'); | ||
checkError.compatibleConstructor(derivedInstance, errorInstance); | ||
`) === true); | ||
assert(runCodeInVm(` | ||
const errorInstance = new Error('I am an instance'); | ||
const derivedInstance = new TypeError('I inherit from Error'); | ||
checkError.compatibleConstructor(errorInstance, derivedInstance); | ||
`) === false); | ||
assert(runCodeInVm(` | ||
const errorInstance = new TypeError('I am an instance'); | ||
checkError.compatibleConstructor(errorInstance, TypeError); | ||
`) === true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export default { | ||
nodeResolve: true, | ||
files: [ | ||
'test/*.js', | ||
'!test/virtual-machines.js', | ||
], | ||
plugins: [ | ||
], | ||
}; |