chore: convert rewriter tests to vitest (#32615)

This commit is contained in:
Bill Glesias
2025-10-02 14:05:00 -04:00
committed by GitHub
parent ed157a3885
commit ca4f515290
14 changed files with 398 additions and 172 deletions
+1 -1
View File
@@ -1784,7 +1784,7 @@ jobs:
source ./scripts/ensure-node.sh
yarn lerna run types
- sanitize-verify-and-store-mocha-results:
expectedResultCount: 13
expectedResultCount: 12
verify-release-readiness:
<<: *defaults
+1 -1
View File
@@ -105,7 +105,7 @@ When migrating some of these projects away from the `ts-node` entry [see `@packa
- [ ] packages/network
- [ ] packages/packherd-require
- [ ] packages/proxy
- [ ] packages/rewriter
- [x] packages/rewriter**COMPLETED**
- [x] packages/scaffold-config ✅ **COMPLETED**
- [ ] packages/server
- [ ] packages/socket
@@ -1,27 +0,0 @@
exports['DeferredSourceMapCache #resolve sourcemap generation for JS with no original sourcemap 1'] = {
"version": 3,
"sources": [
"bar (original)"
],
"names": [],
"mappings": "AAAA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC",
"file": "bar (original).map",
"sourceRoot": ".",
"sourcesContent": [
"console.log()"
]
}
exports['composed sourcemap'] = {
"version": 3,
"sources": [
"test.coffee"
],
"names": [],
"mappings": ";AAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA;EAAA,CAAA,EAAA,CAAA,CAAA,CAAA,EAAA,CAAA,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA,CAAA,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA,CAAA,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA;;;EAIA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAW,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA;IACT,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA;IAAA,CAAA,CAAA,EAAA,CAAA,CAAA,CAAA;WACA,CAAA,CAAA,EAAA,EAAM,CAAA,CAAA,2DAAG,CAAA,CAAA,CAAA,CAAA,CAAA,SAAU,CAAC,CAAA,CAAA,CAAd,CAAA;EAFG,CAAX,EAGE,CAAA,CAAA,CAAA,CAHF,CAAA",
"file": "foo.js (original).map",
"sourceRoot": "http://somedomain.net/dir",
"sourcesContent": [
"# just an example of transpilation w/ sourcemap -\n# `test.coffee` is not directly transpiled/executed by any test code\n# regenerate JS + sourcemap with `coffee -c -m test.coffee`\n\nsetTimeout ->\n window\n foo = \"#{window.top.foo}\"\n, 1000\n"
]
}
@@ -1,18 +0,0 @@
exports['js rewriter .rewriteJs source maps emits sourceInfo as expected 1'] = {
"url": "http://example.com/foo.js",
"js": "window.top"
}
exports['js rewriter .rewriteJs source maps emits info about existing inline sourcemap 1'] = {
"url": "http://example.com/foo.js",
"js": "// Generated by CoffeeScript 2.2.1\n(function() {\n // just an example of transpilation w/ sourcemap -\n // `test.coffee` is not directly transpiled/executed by any test code\n // regenerate JS + sourcemap with `coffee -c -m test.coffee`\n setTimeout(function() {\n window;\n var foo;\n return foo = `${window.top.foo}`;\n }, 1000);\n\n}).call(this);\n\n//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAiZmlsZSI6ICJ0ZXN0LmpzIiwKICAic291cmNlUm9vdCI6ICIiLAogICJzb3VyY2VzIjogWwogICAgInRlc3QuY29mZmVlIgogIF0sCiAgIm5hbWVzIjogW10sCiAgIm1hcHBpbmdzIjogIjtBQUFBO0VBQUE7OztFQUlBLFVBQUEsQ0FBVyxRQUFBLENBQUEsQ0FBQTtJQUNUO0FBQUEsUUFBQTtXQUNBLEdBQUEsR0FBTSxDQUFBLENBQUEsQ0FBRyxNQUFNLENBQUMsR0FBRyxDQUFDLEdBQWQsQ0FBQTtFQUZHLENBQVgsRUFHRSxJQUhGO0FBSkEiLAogICJzb3VyY2VzQ29udGVudCI6IFsKICAgICIjIGp1c3QgYW4gZXhhbXBsZSBvZiB0cmFuc3BpbGF0aW9uIHcvIHNvdXJjZW1hcCAtXG4jIGB0ZXN0LmNvZmZlZWAgaXMgbm90IGRpcmVjdGx5IHRyYW5zcGlsZWQvZXhlY3V0ZWQgYnkgYW55IHRlc3QgY29kZVxuIyByZWdlbmVyYXRlIEpTICsgc291cmNlbWFwIHdpdGggYGNvZmZlZSAtYyAtbSB0ZXN0LmNvZmZlZWBcblxuc2V0VGltZW91dCAtPlxuICB3aW5kb3dcbiAgZm9vID0gXCIje3dpbmRvdy50b3AuZm9vfVwiXG4sIDEwMDBcbiIKICBdCn0="
}
exports['js rewriter .rewriteJs source maps emits info about existing external sourcemap 1'] = {
"url": "http://example.com/foo.js",
"js": "// Generated by CoffeeScript 2.2.1\n(function() {\n // just an example of transpilation w/ sourcemap -\n // `test.coffee` is not directly transpiled/executed by any test code\n // regenerate JS + sourcemap with `coffee -c -m test.coffee`\n setTimeout(function() {\n window;\n var foo;\n return foo = `${window.top.foo}`;\n }, 1000);\n\n}).call(this);\n\n//# sourceMappingURL=test.js.map\n"
}
exports['js rewriter .rewriteJs transformations throws an error via the driver if AST visiting throws an error 1'] = `
window.top.Cypress.utils.throwErrByPath('proxy.js_rewriting_failed', { args: {"errMessage":"foo","errStack":"stack","url":"http://example.com/foo.js"} })
`
+3 -8
View File
@@ -10,14 +10,13 @@
"clean": "rimraf --glob 'lib/**/*.js'",
"clean-deps": "rimraf node_modules",
"lint": "eslint --ext .js,.jsx,.ts,.tsx,.json, .",
"test": "mocha --reporter mocha-multi-reporters --reporter-options configFile=../../mocha-reporter-config.json",
"test": "vitest run",
"test-debug": "vitest --inspect-brk --no-file-parallelism --test-timeout=0",
"tslint": "tslint --config ../ts/tslint.json --project ."
},
"dependencies": {
"ast-types": "0.13.3",
"bluebird": "3.7.2",
"chai": "4.2.0",
"chai-as-promised": "7.1.1",
"debug": "^4.3.4",
"lodash": "^4.17.19",
"parse5-html-rewriting-stream": "5.1.1",
@@ -27,11 +26,7 @@
"@cypress/request-promise": "^5.0.0",
"@types/parse5-html-rewriting-stream": "5.1.1",
"fs-extra": "9.1.0",
"mocha": "3.5.3",
"nock": "13.2.9",
"sinon": "9.0.2",
"sinon-chai": "3.7.0",
"snap-shot-it": "7.9.10"
"vitest": "^3.2.4"
},
"files": [
"lib"
-4
View File
@@ -1,4 +0,0 @@
test/unit/*
-r @packages/ts/register
--timeout 10000
--recursive
@@ -0,0 +1,113 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[`DeferredSourceMapCache > #resolve > sourcemap generation > composition > with external sourceMappingURL 1`] = `
{
"file": "foo.js (original).map",
"mappings": ";AAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA;EAAA,CAAA,EAAA,CAAA,CAAA,CAAA,EAAA,CAAA,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA,CAAA,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA,CAAA,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA;;;EAIA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAW,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA;IACT,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA;IAAA,CAAA,CAAA,EAAA,CAAA,CAAA,CAAA;WACA,CAAA,CAAA,EAAA,EAAM,CAAA,CAAA,2DAAG,CAAA,CAAA,CAAA,CAAA,CAAA,SAAU,CAAC,CAAA,CAAA,CAAd,CAAA;EAFG,CAAX,EAGE,CAAA,CAAA,CAAA,CAHF,CAAA",
"names": [],
"sourceRoot": "http://somedomain.net/dir",
"sources": [
"test.coffee",
],
"sourcesContent": [
"# just an example of transpilation w/ sourcemap -
# \`test.coffee\` is not directly transpiled/executed by any test code
# regenerate JS + sourcemap with \`coffee -c -m test.coffee\`
setTimeout ->
window
foo = "#{window.top.foo}"
, 1000
",
],
"version": 3,
}
`;
exports[`DeferredSourceMapCache > #resolve > sourcemap generation > composition > with inlined base64 sourceMappingURL 1`] = `
{
"file": "foo.js (original).map",
"mappings": ";AAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA;EAAA,CAAA,EAAA,CAAA,CAAA,CAAA,EAAA,CAAA,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA,CAAA,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA,CAAA,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA;;;EAIA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAW,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA;IACT,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA;IAAA,CAAA,CAAA,EAAA,CAAA,CAAA,CAAA;WACA,CAAA,CAAA,EAAA,EAAM,CAAA,CAAA,2DAAG,CAAA,CAAA,CAAA,CAAA,CAAA,SAAU,CAAC,CAAA,CAAA,CAAd,CAAA;EAFG,CAAX,EAGE,CAAA,CAAA,CAAA,CAHF,CAAA",
"names": [],
"sourceRoot": "http://somedomain.net/dir",
"sources": [
"test.coffee",
],
"sourcesContent": [
"# just an example of transpilation w/ sourcemap -
# \`test.coffee\` is not directly transpiled/executed by any test code
# regenerate JS + sourcemap with \`coffee -c -m test.coffee\`
setTimeout ->
window
foo = "#{window.top.foo}"
, 1000
",
],
"version": 3,
}
`;
exports[`DeferredSourceMapCache > #resolve > sourcemap generation > composition > with map referenced by sourcemap header 1`] = `
{
"file": "foo.js (original).map",
"mappings": ";AAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA;EAAA,CAAA,EAAA,CAAA,CAAA,CAAA,EAAA,CAAA,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA,CAAA,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA,CAAA,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA;;;EAIA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAW,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA;IACT,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA;IAAA,CAAA,CAAA,EAAA,CAAA,CAAA,CAAA;WACA,CAAA,CAAA,EAAA,EAAM,CAAA,CAAA,2DAAG,CAAA,CAAA,CAAA,CAAA,CAAA,SAAU,CAAC,CAAA,CAAA,CAAd,CAAA;EAFG,CAAX,EAGE,CAAA,CAAA,CAAA,CAHF,CAAA",
"names": [],
"sourceRoot": "http://somedomain.net/dir",
"sources": [
"test.coffee",
],
"sourcesContent": [
"# just an example of transpilation w/ sourcemap -
# \`test.coffee\` is not directly transpiled/executed by any test code
# regenerate JS + sourcemap with \`coffee -c -m test.coffee\`
setTimeout ->
window
foo = "#{window.top.foo}"
, 1000
",
],
"version": 3,
}
`;
exports[`DeferredSourceMapCache > #resolve > sourcemap generation > composition > with map referenced by x-sourcemap header 1`] = `
{
"file": "foo.js (original).map",
"mappings": ";AAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA;EAAA,CAAA,EAAA,CAAA,CAAA,CAAA,EAAA,CAAA,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA,CAAA,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA,CAAA,EAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA;;;EAIA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAW,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA,EAAA;IACT,CAAA,CAAA,CAAA,CAAA,CAAA,CAAA;IAAA,CAAA,CAAA,EAAA,CAAA,CAAA,CAAA;WACA,CAAA,CAAA,EAAA,EAAM,CAAA,CAAA,2DAAG,CAAA,CAAA,CAAA,CAAA,CAAA,SAAU,CAAC,CAAA,CAAA,CAAd,CAAA;EAFG,CAAX,EAGE,CAAA,CAAA,CAAA,CAHF,CAAA",
"names": [],
"sourceRoot": "http://somedomain.net/dir",
"sources": [
"test.coffee",
],
"sourcesContent": [
"# just an example of transpilation w/ sourcemap -
# \`test.coffee\` is not directly transpiled/executed by any test code
# regenerate JS + sourcemap with \`coffee -c -m test.coffee\`
setTimeout ->
window
foo = "#{window.top.foo}"
, 1000
",
],
"version": 3,
}
`;
exports[`DeferredSourceMapCache > #resolve > sourcemap generation > for JS with no original sourcemap 1`] = `
{
"file": "bar (original).map",
"mappings": "AAAA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC",
"names": [],
"sourceRoot": ".",
"sources": [
"bar (original)",
],
"sourcesContent": [
"console.log()",
],
"version": 3,
}
`;
File diff suppressed because one or more lines are too long
@@ -0,0 +1,51 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[`js rewriter > .rewriteJs > source maps > emits info about existing external sourcemap 1`] = `
{
"js": "// Generated by CoffeeScript 2.2.1
(function() {
// just an example of transpilation w/ sourcemap -
// \`test.coffee\` is not directly transpiled/executed by any test code
// regenerate JS + sourcemap with \`coffee -c -m test.coffee\`
setTimeout(function() {
window;
var foo;
return foo = \`\${window.top.foo}\`;
}, 1000);
}).call(this);
//# sourceMappingURL=test.js.map
",
"url": "http://example.com/foo.js",
}
`;
exports[`js rewriter > .rewriteJs > source maps > emits info about existing inline sourcemap 1`] = `
{
"js": "// Generated by CoffeeScript 2.2.1
(function() {
// just an example of transpilation w/ sourcemap -
// \`test.coffee\` is not directly transpiled/executed by any test code
// regenerate JS + sourcemap with \`coffee -c -m test.coffee\`
setTimeout(function() {
window;
var foo;
return foo = \`\${window.top.foo}\`;
}, 1000);
}).call(this);
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAiZmlsZSI6ICJ0ZXN0LmpzIiwKICAic291cmNlUm9vdCI6ICIiLAogICJzb3VyY2VzIjogWwogICAgInRlc3QuY29mZmVlIgogIF0sCiAgIm5hbWVzIjogW10sCiAgIm1hcHBpbmdzIjogIjtBQUFBO0VBQUE7OztFQUlBLFVBQUEsQ0FBVyxRQUFBLENBQUEsQ0FBQTtJQUNUO0FBQUEsUUFBQTtXQUNBLEdBQUEsR0FBTSxDQUFBLENBQUEsQ0FBRyxNQUFNLENBQUMsR0FBRyxDQUFDLEdBQWQsQ0FBQTtFQUZHLENBQVgsRUFHRSxJQUhGO0FBSkEiLAogICJzb3VyY2VzQ29udGVudCI6IFsKICAgICIjIGp1c3QgYW4gZXhhbXBsZSBvZiB0cmFuc3BpbGF0aW9uIHcvIHNvdXJjZW1hcCAtXG4jIGB0ZXN0LmNvZmZlZWAgaXMgbm90IGRpcmVjdGx5IHRyYW5zcGlsZWQvZXhlY3V0ZWQgYnkgYW55IHRlc3QgY29kZVxuIyByZWdlbmVyYXRlIEpTICsgc291cmNlbWFwIHdpdGggYGNvZmZlZSAtYyAtbSB0ZXN0LmNvZmZlZWBcblxuc2V0VGltZW91dCAtPlxuICB3aW5kb3dcbiAgZm9vID0gXCIje3dpbmRvdy50b3AuZm9vfVwiXG4sIDEwMDBcbiIKICBdCn0=",
"url": "http://example.com/foo.js",
}
`;
exports[`js rewriter > .rewriteJs > source maps > emits sourceInfo as expected 1`] = `
{
"js": "window.top",
"url": "http://example.com/foo.js",
}
`;
exports[`js rewriter > .rewriteJs > transformations > throws an error via the driver if AST visiting throws an error 1`] = `"window.top.Cypress.utils.throwErrByPath('proxy.js_rewriting_failed', { args: {"errMessage":"foo","errStack":"stack","url":"http://example.com/foo.js"} })"`;
@@ -1,36 +1,25 @@
import { describe, expect, it, vi } from 'vitest'
import { DeferredSourceMapCache } from '../../lib/deferred-source-map-cache'
import sinon from 'sinon'
import chai, { expect } from 'chai'
import chaiAsPromised from 'chai-as-promised'
import sinonChai from 'sinon-chai'
import {
testSourceWithExternalSourceMap,
testSourceWithInlineSourceMap,
testSourceMap,
testSourceWithNoSourceMap,
} from '../fixtures'
import snapshot from 'snap-shot-it'
chai.use(chaiAsPromised)
chai.use(sinonChai)
describe('DeferredSourceMapCache', function () {
let cache: DeferredSourceMapCache
beforeEach(() => {
cache = new DeferredSourceMapCache(sinon.stub())
cache = new DeferredSourceMapCache(vi.fn())
})
afterEach(() => {
sinon.restore()
})
context('#defer', () => {
describe('#defer', () => {
it('adds to requests', () => {
const request = { uniqueId: 'foo', url: 'bar' }
cache.defer(request)
expect(cache.requests).to.deep.eq([request])
expect(cache.requests).toEqual([request])
})
it('replaces existing requests for same URL', () => {
@@ -41,25 +30,25 @@ describe('DeferredSourceMapCache', function () {
cache.defer(request0)
cache.defer(request1)
cache.defer(request2)
expect(cache.requests).to.deep.eq([request0, request2])
expect(cache.requests).toEqual([request0, request2])
})
it('throws if uniqueId is duplicated', () => {
cache.defer({ uniqueId: 'foo', url: 'bar' })
expect(() => {
cache.defer({ uniqueId: 'foo', url: 'baz' })
}).to.throw
}).toThrow()
})
})
context('#resolve', () => {
describe('#resolve', () => {
it('rejects if unknown uniqueId', async () => {
cache.defer({
uniqueId: 'baz',
url: 'quux',
})
await expect(cache.resolve('foo', {})).to.be.rejectedWith('Missing request with ID \'foo\'')
await expect(cache.resolve('foo', {})).rejects.toThrow('Missing request with ID \'foo\'')
})
it('rejects if request missing JS', async () => {
@@ -68,10 +57,10 @@ describe('DeferredSourceMapCache', function () {
url: 'bar',
})
await expect(cache.resolve('foo', {})).to.be.rejectedWith(/^Missing JS/)
await expect(cache.resolve('foo', {})).rejects.toThrow(/^Missing JS/)
})
context('sourcemap generation', () => {
describe('sourcemap generation', () => {
it('for JS with no original sourcemap', async () => {
cache.defer({
uniqueId: 'foo',
@@ -80,7 +69,9 @@ describe('DeferredSourceMapCache', function () {
resHeaders: {},
})
snapshot(await cache.resolve('foo', {}))
const result = await cache.resolve('foo', {})
expect(result).toMatchSnapshot()
})
it('resolves with cached sourceMap on retry', async () => {
@@ -94,10 +85,10 @@ describe('DeferredSourceMapCache', function () {
const result0 = await cache.resolve('foo', {})
const result1 = await cache.resolve('foo', {})
expect(result0).to.eq(result1) // same object reference
expect(result0).toEqual(result1) // same object reference
})
context('composition', () => {
describe('composition', () => {
const URL = 'http://somedomain.net/dir/foo.js'
const testExternalSourceMap = (js, resHeaders, expectRequest = true) => {
@@ -110,22 +101,24 @@ describe('DeferredSourceMapCache', function () {
})
// @ts-ignore: https://github.com/bahmutov/snap-shot-it/issues/522
snapshot('composed sourcemap', await cache.resolve('foo', {}), { allowSharedSnapshot: true })
const result = await cache.resolve('foo', {})
expect(result).toMatchSnapshot()
if (!expectRequest) {
return
}
expect(cache.requestLib).to.be.calledWith({
expect(cache.requestLib).toHaveBeenCalledWith({
url: 'http://somedomain.net/dir/test.js.map',
headers: {},
timeout: 5000,
})
}, true)
}
}
beforeEach(() => {
cache.requestLib.resolves({ body: testSourceMap })
cache.requestLib.mockResolvedValue({ body: testSourceMap })
})
it('with inlined base64 sourceMappingURL', testExternalSourceMap(testSourceWithInlineSourceMap, {}, false))
File diff suppressed because one or more lines are too long
@@ -1,17 +1,27 @@
import { describe, expect, it, vi, beforeEach } from 'vitest'
import _ from 'lodash'
import { expect } from 'chai'
import { _rewriteJsUnsafe } from '../../lib/js'
import fse from 'fs-extra'
import Bluebird from 'bluebird'
import rp from '@cypress/request-promise'
import snapshot from 'snap-shot-it'
import * as astTypes from 'ast-types'
import sinon from 'sinon'
import {
testSourceWithExternalSourceMap,
testSourceWithInlineSourceMap,
} from '../fixtures'
vi.mock('ast-types', async (importActual) => {
const actual = await importActual<typeof import('ast-types')>()
return {
...actual,
PathVisitor: {
...actual.PathVisitor,
fromMethodsObject: vi.fn(),
},
}
})
const URL = 'http://example.com/foo.js'
function match (varName, prop) {
@@ -26,17 +36,19 @@ function testExpectedJs (string: string, expected: string) {
// use _rewriteJsUnsafe so exceptions can cause the test to fail
const actual = _rewriteJsUnsafe(URL, string)
expect(actual).to.eq(expected)
expect(actual).toEqual(expected)
}
describe('js rewriter', function () {
afterEach(() => {
sinon.restore()
beforeEach(async () => {
const { PathVisitor } = await vi.importActual<typeof import('ast-types')>('ast-types')
vi.mocked(astTypes.PathVisitor.fromMethodsObject).mockImplementation(PathVisitor.fromMethodsObject)
})
context('.rewriteJs', function () {
context('transformations', function () {
context('injects Cypress window property resolver', () => {
describe('.rewriteJs', function () {
describe('transformations', function () {
describe('injects Cypress window property resolver', () => {
[
['window.top', match('window', 'top')],
['window.parent', match('window', 'parent')],
@@ -164,11 +176,13 @@ describe('js rewriter', function () {
err.stack = 'stack'
sinon.stub(astTypes.PathVisitor, 'fromMethodsObject').throws(err)
vi.mocked(astTypes.PathVisitor.fromMethodsObject).mockImplementation(() => {
throw err
})
const actual = _rewriteJsUnsafe(URL, 'console.log()')
snapshot(actual)
expect(actual).toMatchSnapshot()
})
it('replaces jira window getter', () => {
@@ -290,9 +304,6 @@ describe('js rewriter', function () {
_.each(libs, (url, lib) => {
it(`does not corrupt code from '${lib}'`, function () {
// may have to download and rewrite large files
this.timeout(30000)
const pathToLib = `/tmp/${lib}`
const downloadFile = () => {
@@ -313,36 +324,44 @@ describe('js rewriter', function () {
expect(() => eval(stripped), 'is valid JS').to.not.throw
})
})
// may have to download and rewrite large files
// hence 30 second timeout
}, 30000)
})
})
})
context('source maps', function () {
it('emits sourceInfo as expected', function (done) {
_rewriteJsUnsafe(URL, 'window.top', (sourceInfo) => {
snapshot(sourceInfo)
done()
describe('source maps', function () {
it('emits sourceInfo as expected', function () {
return new Promise<void>((resolve) => {
_rewriteJsUnsafe(URL, 'window.top', (sourceInfo) => {
expect(sourceInfo).toMatchSnapshot()
resolve()
return ''
return ''
})
})
})
it('emits info about existing inline sourcemap', function (done) {
_rewriteJsUnsafe(URL, testSourceWithInlineSourceMap, (sourceInfo) => {
snapshot(sourceInfo)
done()
it('emits info about existing inline sourcemap', function () {
return new Promise<void>((resolve) => {
_rewriteJsUnsafe(URL, testSourceWithInlineSourceMap, (sourceInfo) => {
expect(sourceInfo).toMatchSnapshot()
resolve()
return ''
return ''
})
})
})
it('emits info about existing external sourcemap', function (done) {
_rewriteJsUnsafe(URL, testSourceWithExternalSourceMap, (sourceInfo) => {
snapshot(sourceInfo)
done()
it('emits info about existing external sourcemap', function () {
return new Promise<void>((resolve) => {
_rewriteJsUnsafe(URL, testSourceWithExternalSourceMap, (sourceInfo) => {
expect(sourceInfo).toMatchSnapshot()
resolve()
return ''
return ''
})
})
})
})
+9
View File
@@ -0,0 +1,9 @@
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
include: ['test/**/*.spec.ts'],
globals: true,
environment: 'node',
},
})
+13 -13
View File
@@ -29738,19 +29738,6 @@ sinon@8.1.1:
nise "^3.0.1"
supports-color "^7.1.0"
sinon@9.0.2, sinon@^9.0.0:
version "9.0.2"
resolved "https://registry.yarnpkg.com/sinon/-/sinon-9.0.2.tgz#b9017e24633f4b1c98dfb6e784a5f0509f5fd85d"
integrity sha512-0uF8Q/QHkizNUmbK3LRFqx5cpTttEVXudywY9Uwzy8bTfZUhljZ7ARzSxnRHWYWtVTeh4Cw+tTb3iU21FQVO9A==
dependencies:
"@sinonjs/commons" "^1.7.2"
"@sinonjs/fake-timers" "^6.0.1"
"@sinonjs/formatio" "^5.0.1"
"@sinonjs/samsam" "^5.0.3"
diff "^4.0.2"
nise "^4.0.1"
supports-color "^7.1.0"
sinon@^10.0.0:
version "10.0.0"
resolved "https://registry.yarnpkg.com/sinon/-/sinon-10.0.0.tgz#52279f97e35646ff73d23207d0307977c9b81430"
@@ -29763,6 +29750,19 @@ sinon@^10.0.0:
nise "^4.1.0"
supports-color "^7.1.0"
sinon@^9.0.0:
version "9.0.2"
resolved "https://registry.yarnpkg.com/sinon/-/sinon-9.0.2.tgz#b9017e24633f4b1c98dfb6e784a5f0509f5fd85d"
integrity sha512-0uF8Q/QHkizNUmbK3LRFqx5cpTttEVXudywY9Uwzy8bTfZUhljZ7ARzSxnRHWYWtVTeh4Cw+tTb3iU21FQVO9A==
dependencies:
"@sinonjs/commons" "^1.7.2"
"@sinonjs/fake-timers" "^6.0.1"
"@sinonjs/formatio" "^5.0.1"
"@sinonjs/samsam" "^5.0.3"
diff "^4.0.2"
nise "^4.0.1"
supports-color "^7.1.0"
sirv@^2.0.3, sirv@^2.0.4:
version "2.0.4"
resolved "https://registry.yarnpkg.com/sirv/-/sirv-2.0.4.tgz#5dd9a725c578e34e449f332703eb2a74e46a29b0"