From 8dec68ec92a78ff74e1e1ae2568dbafb8078451a Mon Sep 17 00:00:00 2001 From: Hikari Hayashi Date: Wed, 17 Jul 2024 23:13:53 +0800 Subject: [PATCH] feat: support babel v8 --- package.json | 1 + src/index.js | 2 +- test/babel-plugin-istanbul.js | 535 +++++++++++++++++----------------- 3 files changed, 275 insertions(+), 263 deletions(-) diff --git a/package.json b/package.json index 79604bd..1d0bd09 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "devDependencies": { "@babel/cli": "^7.24.1", "@babel/core": "^7.24.1", + "@babel/core-v8": "npm:@babel/core@8.0.0-alpha.11", "@babel/plugin-transform-block-scoping": "^7.24.1", "@babel/plugin-transform-modules-commonjs": "^7.24.1", "@babel/register": "^7.23.7", diff --git a/src/index.js b/src/index.js index 56b636a..add267a 100644 --- a/src/index.js +++ b/src/index.js @@ -94,7 +94,7 @@ function makeShouldSkip () { } export default declare(api => { - api.assertVersion(7) + api.assertVersion('^7.0.0-0 || ^8.0.0-0') const shouldSkip = makeShouldSkip() diff --git a/test/babel-plugin-istanbul.js b/test/babel-plugin-istanbul.js index 86d363e..69bf13c 100644 --- a/test/babel-plugin-istanbul.js +++ b/test/babel-plugin-istanbul.js @@ -1,49 +1,27 @@ /* eslint-env mocha */ import * as babel from '@babel/core' +import * as babelV8 from '@babel/core-v8' import makeVisitor from '../src' import path from 'path' require('chai').should() -describe('babel-plugin-istanbul', function () { - context('Babel plugin config', function () { - it('should instrument file if shouldSkip returns false', function () { - var result = babel.transformFileSync('./fixtures/plugin-should-cover.js', { - babelrc: false, - configFile: false, - plugins: [ - [makeVisitor, { - include: ['fixtures/plugin-should-cover.js'] - }] - ] - }) - result.code.should.match(/statementMap/) - }) +const babelVersions = [ + { version: 7, babel }, + { version: 8, babel: babelV8 } +] - it('should not instrument file if shouldSkip returns true', function () { - var result = babel.transformFileSync('./fixtures/plugin-should-not-cover.js', { - babelrc: false, - configFile: false, - plugins: [ - [makeVisitor, { - include: ['fixtures/plugin-should-cover.js'] - }] - ] - }) - result.code.should.not.match(/statementMap/) - }) - - context('local node_modules', function () { +for (const { version, babel } of babelVersions) { + describe(`babel-plugin-istanbul with babel v${version}`, function () { + context('Babel plugin config', function () { it('should instrument file if shouldSkip returns false', function () { - var result = babel.transformFileSync('./fixtures/node_modules/should-cover.js', { + var result = babel.transformFileSync('./fixtures/plugin-should-cover.js', { babelrc: false, configFile: false, plugins: [ [makeVisitor, { - excludeNodeModules: false, - exclude: ['node_modules/**'], - include: ['fixtures/node_modules/should-cover.js'] + include: ['fixtures/plugin-should-cover.js'] }] ] }) @@ -51,296 +29,329 @@ describe('babel-plugin-istanbul', function () { }) it('should not instrument file if shouldSkip returns true', function () { - var result = babel.transformFileSync('./fixtures/node_modules/should-not-cover.js', { + var result = babel.transformFileSync('./fixtures/plugin-should-not-cover.js', { babelrc: false, configFile: false, plugins: [ [makeVisitor, { - include: ['fixtures/node_modules/should-not-cover.js'] + include: ['fixtures/plugin-should-cover.js'] }] ] }) result.code.should.not.match(/statementMap/) }) - }) - - it('should call onCover callback', function () { - var args - babel.transformFileSync('./fixtures/plugin-should-cover.js', { - babelrc: false, - configFile: false, - plugins: [ - [makeVisitor, { - onCover: function () { - args = [].slice.call(arguments) - }, - include: ['fixtures/plugin-should-cover.js'] - }] - ] - }) - args[0].should.equal(path.resolve('./fixtures/plugin-should-cover.js')) - args[1].statementMap.should.exist // eslint-disable-line - }) - }) - - context('source maps', function () { - it('should use inline source map', function () { - var result = babel.transformFileSync('./fixtures/has-inline-source-map.js', { - babelrc: false, - configFile: false, - plugins: [ - [makeVisitor, { - include: ['fixtures/has-inline-source-map.js'] - }] - ] - }) - result.code.should.match(/inputSourceMap/) - }) - it('should not use inline source map if inputSourceMap is set to false', function () { - var result = babel.transformFileSync('./fixtures/has-inline-source-map.js', { - babelrc: false, - configFile: false, - plugins: [ - [makeVisitor, { - include: ['fixtures/has-inline-source-map.js'], - useInlineSourceMaps: false - }] - ] - }) - result.code.should.not.match(/inputSourceMap/) - }) + context('local node_modules', function () { + it('should instrument file if shouldSkip returns false', function () { + var result = babel.transformFileSync('./fixtures/node_modules/should-cover.js', { + babelrc: false, + configFile: false, + plugins: [ + [makeVisitor, { + excludeNodeModules: false, + exclude: ['node_modules/**'], + include: ['fixtures/node_modules/should-cover.js'] + }] + ] + }) + result.code.should.match(/statementMap/) + }) - it('should use provided source map', function () { - var result = babel.transformFileSync('./fixtures/has-inline-source-map.js', { - babelrc: false, - configFile: false, - plugins: [ - [makeVisitor, { - include: ['fixtures/has-inline-source-map.js'], - inputSourceMap: { asdfQwer: 'foo' } - }] - ] + it('should not instrument file if shouldSkip returns true', function () { + var result = babel.transformFileSync('./fixtures/node_modules/should-not-cover.js', { + babelrc: false, + configFile: false, + plugins: [ + [makeVisitor, { + include: ['fixtures/node_modules/should-not-cover.js'] + }] + ] + }) + result.code.should.not.match(/statementMap/) + }) }) - result.code.should.match(/inputSourceMap:\s*{\s*asdfQwer: "foo"\s*}/) - }) - }) - context('instrument options', function () { - it('should honor coverageVariable option', function () { - const result = babel.transformFileSync('./fixtures/should-cover.js', { - babelrc: false, - configFile: false, - plugins: [ - [makeVisitor, { - include: ['fixtures/should-cover.js'], - coverageVariable: '__TEST_VARIABLE__' - }] - ] + it('should call onCover callback', function () { + var args + babel.transformFileSync('./fixtures/plugin-should-cover.js', { + babelrc: false, + configFile: false, + plugins: [ + [makeVisitor, { + onCover: function () { + args = [].slice.call(arguments) + }, + include: ['fixtures/plugin-should-cover.js'] + }] + ] + }) + args[0].should.equal(path.resolve('./fixtures/plugin-should-cover.js')) + args[1].statementMap.should.exist // eslint-disable-line }) - result.code.should.match(/__TEST_VARIABLE__/) - result.code.should.not.match(/__coverage__/) }) - it('should honor coverageGlobalScope option', function () { - const result = babel.transformFileSync('./fixtures/should-cover.js', { - babelrc: false, - configFile: false, - plugins: [ - [makeVisitor, { - include: ['fixtures/should-cover.js'], - coverageGlobalScope: 'window' - }] - ] + context('source maps', function () { + it('should use inline source map', function () { + var result = babel.transformFileSync('./fixtures/has-inline-source-map.js', { + babelrc: false, + configFile: false, + plugins: [ + [makeVisitor, { + include: ['fixtures/has-inline-source-map.js'] + }] + ] + }) + result.code.should.match(/inputSourceMap/) }) - result.code.should.match(/new Function\("return window"\)/) - result.code.should.not.match(/new Function\("return this"\)/) - }) - it('should honor coverageGlobalScope option', function () { - const result = babel.transformFileSync('./fixtures/should-cover.js', { - babelrc: false, - configFile: false, - plugins: [ - [makeVisitor, { - include: ['fixtures/should-cover.js'], - coverageGlobalScopeFunc: false - }] - ] + it('should not use inline source map if inputSourceMap is set to false', function () { + var result = babel.transformFileSync('./fixtures/has-inline-source-map.js', { + babelrc: false, + configFile: false, + plugins: [ + [makeVisitor, { + include: ['fixtures/has-inline-source-map.js'], + useInlineSourceMaps: false + }] + ] + }) + result.code.should.not.match(/inputSourceMap/) }) - result.code.should.match(/global\s*=\s*this/) - result.code.should.not.match(/global\s*=\s*new Function\("return this"\)/) - }) - it('should honor ignoreClassMethods option', function () { - const result = babel.transformFileSync('./fixtures/class-functions.js', { - babelrc: false, - configFile: false, - plugins: [ - [makeVisitor, { - include: ['fixtures/class-functions.js'], - ignoreClassMethods: ['bar'] - }] - ] + it('should use provided source map', function () { + var result = babel.transformFileSync('./fixtures/has-inline-source-map.js', { + babelrc: false, + configFile: false, + plugins: [ + [makeVisitor, { + include: ['fixtures/has-inline-source-map.js'], + inputSourceMap: { asdfQwer: 'foo' } + }] + ] + }) + result.code.should.match(/inputSourceMap:\s*{\s*asdfQwer: "foo"\s*}/) }) - - // bar() is ignored - result.code.should.match(/bar\(\)\s*{\s*}/) - result.code.should.not.match(/bar\(\)\s*{\s*cov_.*/) - - // barz() does not get instrumented - result.code.should.match(/barz\(\)\s*{\s*cov_.*/) - result.code.should.not.match(/barz\(\)\s*{\s*}/) }) - }) - context('package.json "nyc" config', function () { - context('process.env.NYC_CONFIG is set', function () { - it('should instrument file if shouldSkip returns false', function () { - var result = babel.transformFileSync('./fixtures/should-cover.js', { + context('instrument options', function () { + it('should honor coverageVariable option', function () { + const result = babel.transformFileSync('./fixtures/should-cover.js', { babelrc: false, configFile: false, plugins: [ - makeVisitor + [makeVisitor, { + include: ['fixtures/should-cover.js'], + coverageVariable: '__TEST_VARIABLE__' + }] ] }) - result.code.should.match(/statementMap/) + result.code.should.match(/__TEST_VARIABLE__/) + result.code.should.not.match(/__coverage__/) }) - it('should not instrument file if shouldSkip returns true', function () { - var result = babel.transformFileSync('./fixtures/should-not-cover.js', { + it('should honor coverageGlobalScope option', function () { + const result = babel.transformFileSync('./fixtures/should-cover.js', { babelrc: false, configFile: false, plugins: [ - makeVisitor + [makeVisitor, { + include: ['fixtures/should-cover.js'], + coverageGlobalScope: 'window' + }] ] }) - result.code.should.not.match(/statementMap/) + result.code.should.match(/new Function\("return window"\)/) + result.code.should.not.match(/new Function\("return this"\)/) }) - }) - context('process.env.NYC_CONFIG is not set', function () { - this.timeout(10000) - - const OLD_NYC_CONFIG = process.env.NYC_CONFIG - const OLD_NYC_CWD = process.env.NYC_CWD - before(() => { - delete process.env.NYC_CONFIG - delete process.env.NYC_CWD - }) - after(() => { - process.env.NYC_CONFIG = OLD_NYC_CONFIG - process.env.NYC_CWD = OLD_NYC_CWD - }) - - it('should instrument file if shouldSkip returns false', function () { - var result = babel.transformFileSync('./fixtures/should-cover.js', { + it('should honor coverageGlobalScope option', function () { + const result = babel.transformFileSync('./fixtures/should-cover.js', { babelrc: false, configFile: false, plugins: [ - makeVisitor + [makeVisitor, { + include: ['fixtures/should-cover.js'], + coverageGlobalScopeFunc: false + }] ] }) - result.code.should.match(/statementMap/) + result.code.should.match(/global\s*=\s*this/) + result.code.should.not.match(/global\s*=\s*new Function\("return this"\)/) }) - it('should not instrument file if shouldSkip returns true', function () { - var result = babel.transformFileSync('./fixtures/should-not-cover.js', { + it('should honor ignoreClassMethods option', function () { + const result = babel.transformFileSync('./fixtures/class-functions.js', { babelrc: false, configFile: false, plugins: [ - makeVisitor + [makeVisitor, { + include: ['fixtures/class-functions.js'], + ignoreClassMethods: ['bar'] + }] ] }) - result.code.should.not.match(/statementMap/) + + // bar() is ignored + result.code.should.match(/bar\(\)\s*{\s*}/) + result.code.should.not.match(/bar\(\)\s*{\s*cov_.*/) + + // barz() does not get instrumented + result.code.should.match(/barz\(\)\s*{\s*cov_.*/) + result.code.should.not.match(/barz\(\)\s*{\s*}/) }) + }) - it('should load config using cwd', function () { - const cwd = path.resolve(__dirname, '..', 'fixtures', 'config') - function helper (file, match, opts) { - const result = babel.transformFileSync( - path.resolve(cwd, file), - { - babelrc: false, - configFile: false, - plugins: [ - [makeVisitor, { cwd, ...opts }] - ] + context('package.json "nyc" config', function () { + context('process.env.NYC_CONFIG is set', function () { + it('should instrument file if shouldSkip returns false', function () { + var result = babel.transformFileSync('./fixtures/should-cover.js', { + babelrc: false, + configFile: false, + plugins: [ + makeVisitor + ] + }) + result.code.should.match(/statementMap/) + }) + + it('should not instrument file if shouldSkip returns true', function () { + var result = babel.transformFileSync('./fixtures/should-not-cover.js', { + babelrc: false, + configFile: false, + plugins: [ + makeVisitor + ] + }) + result.code.should.not.match(/statementMap/) + }) + }) + + context('process.env.NYC_CONFIG is not set', function () { + this.timeout(10000) + + const OLD_NYC_CONFIG = process.env.NYC_CONFIG + const OLD_NYC_CWD = process.env.NYC_CWD + before(() => { + delete process.env.NYC_CONFIG + delete process.env.NYC_CWD + }) + after(() => { + process.env.NYC_CONFIG = OLD_NYC_CONFIG + process.env.NYC_CWD = OLD_NYC_CWD + }) + + it('should instrument file if shouldSkip returns false', function () { + var result = babel.transformFileSync('./fixtures/should-cover.js', { + babelrc: false, + configFile: false, + plugins: [ + makeVisitor + ] + }) + result.code.should.match(/statementMap/) + }) + + it('should not instrument file if shouldSkip returns true', function () { + var result = babel.transformFileSync('./fixtures/should-not-cover.js', { + babelrc: false, + configFile: false, + plugins: [ + makeVisitor + ] + }) + result.code.should.not.match(/statementMap/) + }) + + it('should load config using cwd', function () { + const cwd = path.resolve(__dirname, '..', 'fixtures', 'config') + function helper (file, match, opts) { + const result = babel.transformFileSync( + path.resolve(cwd, file), + { + babelrc: false, + configFile: false, + plugins: [ + [makeVisitor, { cwd, ...opts }] + ] + } + ) + if (match) { + result.code.should.match(/statementMap/) + } else { + result.code.should.not.match(/statementMap/) } - ) - if (match) { - result.code.should.match(/statementMap/) - } else { - result.code.should.not.match(/statementMap/) } - } - helper('file1.js', true) - helper('file2.js', false) - helper('file1.js', false, { nycrcPath: 'nyc-alt.config.js' }) - helper('file2.js', true, { nycrcPath: 'nyc-alt.config.js' }) - ;(function () { - babel.transformFileSync( - path.resolve(cwd, 'file1.js'), - { - babelrc: false, - configFile: false, - plugins: [ - [makeVisitor, { cwd, nycrcPath: 'missing-config.js' }] - ] - } - ) - }).should.throw(/Requested configuration file missing-config.js not found/) + helper('file1.js', true) + helper('file2.js', false) + helper('file1.js', false, { nycrcPath: 'nyc-alt.config.js' }) + helper('file2.js', true, { nycrcPath: 'nyc-alt.config.js' }) + ;(function () { + babel.transformFileSync( + path.resolve(cwd, 'file1.js'), + { + babelrc: false, + configFile: false, + plugins: [ + [makeVisitor, { cwd, nycrcPath: 'missing-config.js' }] + ] + } + ) + }).should.throw(/Requested configuration file missing-config.js not found/) + }) }) }) - }) - // TODO: setup istanbul-lib-instrument, such that we can - // run various babel configurations. - context('regression tests', () => { + // TODO: setup istanbul-lib-instrument, such that we can + // run various babel configurations. + context('regression tests', () => { // regression test for https://github.com/istanbuljs/babel-plugin-istanbul/issues/78 - it('should instrument: export const foo = () => {}', function () { - var result = babel.transformFileSync('./fixtures/issue-78.js', { - babelrc: false, - configFile: false, - plugins: [ - [makeVisitor, { - include: ['fixtures/issue-78.js'] - }] - ] + it('should instrument: export const foo = () => {}', function () { + var result = babel.transformFileSync('./fixtures/issue-78.js', { + babelrc: false, + configFile: false, + plugins: [ + [makeVisitor, { + include: ['fixtures/issue-78.js'] + }] + ] + }) + result.code.match(/statementMap/) }) - result.code.match(/statementMap/) - }) - // regression test for https://github.com/istanbuljs/babel-plugin-istanbul/issues/201 - it('should not conflict with transform-modules-commonjs', function () { - var result = babel.transformFileSync('./fixtures/issue-201.js', { - babelrc: false, - configFile: false, - plugins: [ - [makeVisitor, { - include: ['fixtures/issue-201.js'] - }], - '@babel/plugin-transform-modules-commonjs' - ] - }) - result.code.should.match(/_path.*\.resolve\)\(_path\)/) - result.code.should.not.match(/_path\.resolve\)\(_path\)/) - }) + // TODO: run these tests against babel v8 + if (version === 7) { + // regression test for https://github.com/istanbuljs/babel-plugin-istanbul/issues/201 + it('should not conflict with transform-modules-commonjs', function () { + var result = babel.transformFileSync('./fixtures/issue-201.js', { + babelrc: false, + configFile: false, + plugins: [ + [makeVisitor, { + include: ['fixtures/issue-201.js'] + }], + '@babel/plugin-transform-modules-commonjs' + ] + }) + result.code.should.match(/_path.*\.resolve\)\(_path\)/) + result.code.should.not.match(/_path\.resolve\)\(_path\)/) + }) - // regression test for https://github.com/istanbuljs/babel-plugin-istanbul/issues/289 - it('should instrument: for (let f; ; ) { ...', function () { - var result = babel.transformFileSync('./fixtures/issue-289.js', { - babelrc: false, - configFile: false, - plugins: [ - '@babel/plugin-transform-block-scoping', - [makeVisitor, { - include: ['fixtures/issue-289.js'] - }] - ] - }) - console.log(result) + // regression test for https://github.com/istanbuljs/babel-plugin-istanbul/issues/289 + it('should instrument: for (let f; ; ) { ...', function () { + var result = babel.transformFileSync('./fixtures/issue-289.js', { + babelrc: false, + configFile: false, + plugins: [ + '@babel/plugin-transform-block-scoping', + [makeVisitor, { + include: ['fixtures/issue-289.js'] + }] + ] + }) + console.log(result) + }) + } }) }) -}) +}