| 1 | import { runCodeInTestContext } from "../lib/index.js"; |
| 2 | import { fileURLToPath } from "url"; |
| 3 | |
| 4 | const filename = fileURLToPath(import.meta.url); |
| 5 | |
| 6 | describe("helper-transform-fixture-test-runner", function () { |
| 7 | it("should not execute code in Node's global context", function () { |
| 8 | try { |
| 9 | global.foo = "outer"; |
| 10 | runCodeInTestContext( |
| 11 | ` |
| 12 | expect(global.foo).toBeUndefined(); |
| 13 | global.foo = "inner"; |
| 14 | `, |
| 15 | { |
| 16 | filename: `${filename}.fake1`, |
| 17 | }, |
| 18 | ); |
| 19 | |
| 20 | expect(global.foo).toBe("outer"); |
| 21 | runCodeInTestContext( |
| 22 | ` |
| 23 | expect(global.foo).toBe("inner"); |
| 24 | `, |
| 25 | { |
| 26 | filename: `${filename}.fake2`, |
| 27 | }, |
| 28 | ); |
| 29 | } finally { |
| 30 | delete global.foo; |
| 31 | runCodeInTestContext( |
| 32 | ` |
| 33 | delete global.foo; |
| 34 | `, |
| 35 | { |
| 36 | filename: `${filename}.fake3`, |
| 37 | }, |
| 38 | ); |
| 39 | } |
| 40 | }); |
| 41 | it("should print correct trace position when error is thrown in the first line", () => { |
| 42 | const opts = { |
| 43 | filename: `${filename}.fake4`, |
| 44 | }; |
| 45 | runCodeInTestContext( |
| 46 | `try { throw new Error() } catch (e) { |
| 47 | opts.stack = e.stack |
| 48 | } |
| 49 | `, |
| 50 | opts, |
| 51 | ); |
| 52 | expect(opts.stack).toContain(opts.filename + ":1:13"); |
| 53 | }); |
| 54 | }); |