Skip to content

Commit

Permalink
Improve code quality by increasing code coverage: lib/source-map-from…
Browse files Browse the repository at this point in the history
…-file.js (bcoe#453)

refactor:  lib/source-map-from-file.js to improve code coverage (bcoe#453)
refactor: exposed source-map-from-file.js function to write test cases (bcoe#453)
test: Added two test case cases to cover error handling for improper formating (bcoe#453)
  • Loading branch information
mcknasty committed Feb 10, 2023
1 parent 2a3d0c7 commit 67ad118
Show file tree
Hide file tree
Showing 7 changed files with 1,019 additions and 3,654 deletions.
2 changes: 1 addition & 1 deletion lib/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const reports = require('istanbul-reports')
const { readdirSync, readFileSync, statSync } = require('fs')
const { isAbsolute, resolve, extname } = require('path')
const { pathToFileURL, fileURLToPath } = require('url')
const getSourceMapFromFile = require('./source-map-from-file')
const { getSourceMapFromFile } = require('./source-map-from-file')
// TODO: switch back to @c88/v8-coverage once patch is landed.
const v8toIstanbul = require('v8-to-istanbul')
const isCjsEsmBridgeCov = require('./is-cjs-esm-bridge')
Expand Down
10 changes: 9 additions & 1 deletion lib/source-map-from-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,11 @@ function sourceMapFromDataUrl (url) {
const { 0: format, 1: data } = url.split(',')
const splitFormat = format.split(';')
const contentType = splitFormat[0]
// always evaluates to true
const base64 = splitFormat[splitFormat.length - 1] === 'base64'
if (contentType === 'application/json') {
// the data variable is never a json string in the test cases
/* c8 ignore next 1 */
const decodedData = base64 ? Buffer.from(data, 'base64').toString('utf8') : data
try {
return JSON.parse(decodedData)
Expand All @@ -97,4 +100,9 @@ function sourceMapFromDataUrl (url) {
}
}

module.exports = getSourceMapFromFile
module.exports = {
getSourceMapFromFile,
dataFromUrl,
sourceMapFromFile,
sourceMapFromDataUrl
}
4,566 changes: 938 additions & 3,628 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"dependencies": {
"@bcoe/v8-coverage": "^0.2.3",
"@istanbuljs/schema": "^0.1.3",
"chai-spies": "^1.0.0",
"find-up": "^5.0.0",
"foreground-child": "^2.0.0",
"istanbul-lib-coverage": "^3.2.0",
Expand Down
8 changes: 6 additions & 2 deletions test/integration.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ hey
---------------------------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
---------------------------------------|---------|----------|---------|---------|-------------------
All files | 1.91 | 12 | 6.25 | 1.91 |
All files | 1.61 | 11.32 | 5.88 | 1.61 |
c8 | 0 | 0 | 0 | 0 |
index.js | 0 | 0 | 0 | 0 | 1
c8/bin | 0 | 0 | 0 | 0 |
Expand All @@ -165,11 +165,15 @@ All files | 1.91 | 12 | 6.25 | 1.91
block-navigation.js | 0 | 0 | 0 | 0 | 1-87
prettify.js | 0 | 0 | 0 | 0 | 1-2
sorter.js | 0 | 0 | 0 | 0 | 1-196
c8/coverage/lcov-report | 0 | 0 | 0 | 0 |
block-navigation.js | 0 | 0 | 0 | 0 | 1-87
prettify.js | 0 | 0 | 0 | 0 | 1-2
sorter.js | 0 | 0 | 0 | 0 | 1-196
c8/lib | 0 | 0 | 0 | 0 |
is-cjs-esm-bridge.js | 0 | 0 | 0 | 0 | 1-10
parse-args.js | 0 | 0 | 0 | 0 | 1-218
report.js | 0 | 0 | 0 | 0 | 1-340
source-map-from-file.js | 0 | 0 | 0 | 0 | 1-100
source-map-from-file.js | 0 | 0 | 0 | 0 | 1-108
c8/lib/commands | 0 | 0 | 0 | 0 |
check-coverage.js | 0 | 0 | 0 | 0 | 1-70
report.js | 0 | 0 | 0 | 0 | 1-41
Expand Down
46 changes: 25 additions & 21 deletions test/integration.js_10.snap
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ hey
---------------------------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
---------------------------------------|---------|----------|---------|---------|-------------------
All files | 1.91 | 12 | 8.16 | 1.91 |
All files | 1.61 | 11.32 | 7.69 | 1.61 |
c8 | 0 | 0 | 0 | 0 |
index.js | 0 | 0 | 0 | 0 | 1
c8/bin | 0 | 0 | 0 | 0 |
Expand All @@ -165,11 +165,15 @@ All files | 1.91 | 12 | 8.16 | 1.91
block-navigation.js | 0 | 0 | 0 | 0 | 1-87
prettify.js | 0 | 0 | 0 | 0 | 1-2
sorter.js | 0 | 0 | 0 | 0 | 1-196
c8/coverage/lcov-report | 0 | 0 | 0 | 0 |
block-navigation.js | 0 | 0 | 0 | 0 | 1-87
prettify.js | 0 | 0 | 0 | 0 | 1-2
sorter.js | 0 | 0 | 0 | 0 | 1-196
c8/lib | 0 | 0 | 0 | 0 |
is-cjs-esm-bridge.js | 0 | 0 | 0 | 0 | 1-10
parse-args.js | 0 | 0 | 0 | 0 | 1-218
report.js | 0 | 0 | 0 | 0 | 1-340
source-map-from-file.js | 0 | 0 | 0 | 0 | 1-100
source-map-from-file.js | 0 | 0 | 0 | 0 | 1-108
c8/lib/commands | 0 | 0 | 0 | 0 |
check-coverage.js | 0 | 0 | 0 | 0 | 1-70
report.js | 0 | 0 | 0 | 0 | 1-41
Expand Down Expand Up @@ -257,24 +261,24 @@ hey
--------------------------|---------|----------|---------|---------|--------------------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------------------|---------|----------|---------|---------|--------------------------------
All files | 74.04 | 58.82 | 63.41 | 74.04 |
All files | 74.17 | 58.82 | 63.41 | 74.17 |
bin | 78.84 | 60 | 66.66 | 78.84 |
c8.js | 78.84 | 60 | 66.66 | 78.84 | 22,27-29,32-33,41-43,50-51
lib | 78.59 | 54.23 | 73.07 | 78.59 |
lib | 78.69 | 54.23 | 73.07 | 78.69 |
is-cjs-esm-bridge.js | 90 | 25 | 100 | 90 | 9
parse-args.js | 97.24 | 58.33 | 100 | 97.24 | 159-160,181-182,195-196
report.js | 76.17 | 57.89 | 80 | 76.17 | ...284,290-292,313-318,329-330
source-map-from-file.js | 45 | 100 | 0 | 45 | 39-50,52-67,69-77,81-98
source-map-from-file.js | 48.14 | 100 | 0 | 48.14 | 39-50,52-67,69-77,81-88,91-101
lib/commands | 41.44 | 66.66 | 16.66 | 41.44 |
check-coverage.js | 18.57 | 100 | 0 | 18.57 | 9-11,14-36,39-53,55-70
report.js | 80.48 | 62.5 | 50 | 80.48 | 9-10,15-20
test/fixtures | 83.33 | 85.71 | 66.66 | 83.33 |
async.js | 100 | 100 | 100 | 100 |
normal.js | 75 | 66.66 | 33.33 | 75 | 14-16,18-20
--------------------------|---------|----------|---------|---------|--------------------------------
,ERROR: Coverage for lines (74.04%) does not meet global threshold (101%)
,ERROR: Coverage for lines (74.17%) does not meet global threshold (101%)
ERROR: Coverage for branches (58.82%) does not meet global threshold (82%)
ERROR: Coverage for statements (74.04%) does not meet global threshold (95%)
ERROR: Coverage for statements (74.17%) does not meet global threshold (95%)
"
`;

Expand All @@ -295,8 +299,8 @@ ERROR: Coverage for branches (58.33%) does not meet threshold (82%) for lib/pars
ERROR: Coverage for lines (76.17%) does not meet threshold (101%) for lib/report.js
ERROR: Coverage for branches (57.89%) does not meet threshold (82%) for lib/report.js
ERROR: Coverage for statements (76.17%) does not meet threshold (95%) for lib/report.js
ERROR: Coverage for lines (45%) does not meet threshold (101%) for lib/source-map-from-file.js
ERROR: Coverage for statements (45%) does not meet threshold (95%) for lib/source-map-from-file.js
ERROR: Coverage for lines (48.14%) does not meet threshold (101%) for lib/source-map-from-file.js
ERROR: Coverage for statements (48.14%) does not meet threshold (95%) for lib/source-map-from-file.js
ERROR: Coverage for lines (100%) does not meet threshold (101%) for test/fixtures/async.js
ERROR: Coverage for lines (75%) does not meet threshold (101%) for test/fixtures/normal.js
ERROR: Coverage for branches (66.66%) does not meet threshold (82%) for test/fixtures/normal.js
Expand All @@ -305,19 +309,19 @@ ERROR: Coverage for statements (75%) does not meet threshold (95%) for test/fixt
`;

exports[`c8 check-coverage check-coverage command with --100 1`] = `
",,ERROR: Coverage for lines (77.73%) does not meet global threshold (100%)
",,ERROR: Coverage for lines (77.82%) does not meet global threshold (100%)
ERROR: Coverage for functions (67.44%) does not meet global threshold (100%)
ERROR: Coverage for branches (62.06%) does not meet global threshold (100%)
ERROR: Coverage for statements (77.73%) does not meet global threshold (100%)
ERROR: Coverage for statements (77.82%) does not meet global threshold (100%)
"
`;

exports[`c8 check-coverage exits with 0 if coverage within threshold 1`] = `",,"`;

exports[`c8 check-coverage exits with 1 if coverage is below threshold 1`] = `
",,ERROR: Coverage for lines (74.04%) does not meet global threshold (101%)
",,ERROR: Coverage for lines (74.17%) does not meet global threshold (101%)
ERROR: Coverage for branches (58.82%) does not meet global threshold (82%)
ERROR: Coverage for statements (74.04%) does not meet global threshold (95%)
ERROR: Coverage for statements (74.17%) does not meet global threshold (95%)
"
`;

Expand Down Expand Up @@ -404,14 +408,14 @@ exports[`c8 report generates report from existing temporary files 1`] = `
",--------------------------|---------|----------|---------|---------|--------------------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------------------|---------|----------|---------|---------|--------------------------------
All files | 74.04 | 58.82 | 63.41 | 74.04 |
All files | 74.17 | 58.82 | 63.41 | 74.17 |
bin | 78.84 | 60 | 66.66 | 78.84 |
c8.js | 78.84 | 60 | 66.66 | 78.84 | 22,27-29,32-33,41-43,50-51
lib | 78.59 | 54.23 | 73.07 | 78.59 |
lib | 78.69 | 54.23 | 73.07 | 78.69 |
is-cjs-esm-bridge.js | 90 | 25 | 100 | 90 | 9
parse-args.js | 97.24 | 58.33 | 100 | 97.24 | 159-160,181-182,195-196
report.js | 76.17 | 57.89 | 80 | 76.17 | ...284,290-292,313-318,329-330
source-map-from-file.js | 45 | 100 | 0 | 45 | 39-50,52-67,69-77,81-98
source-map-from-file.js | 48.14 | 100 | 0 | 48.14 | 39-50,52-67,69-77,81-88,91-101
lib/commands | 41.44 | 66.66 | 16.66 | 41.44 |
check-coverage.js | 18.57 | 100 | 0 | 18.57 | 9-11,14-36,39-53,55-70
report.js | 80.48 | 62.5 | 50 | 80.48 | 9-10,15-20
Expand All @@ -426,24 +430,24 @@ exports[`c8 report supports --check-coverage, when generating reports 1`] = `
",--------------------------|---------|----------|---------|---------|--------------------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------------------|---------|----------|---------|---------|--------------------------------
All files | 74.04 | 58.82 | 63.41 | 74.04 |
All files | 74.17 | 58.82 | 63.41 | 74.17 |
bin | 78.84 | 60 | 66.66 | 78.84 |
c8.js | 78.84 | 60 | 66.66 | 78.84 | 22,27-29,32-33,41-43,50-51
lib | 78.59 | 54.23 | 73.07 | 78.59 |
lib | 78.69 | 54.23 | 73.07 | 78.69 |
is-cjs-esm-bridge.js | 90 | 25 | 100 | 90 | 9
parse-args.js | 97.24 | 58.33 | 100 | 97.24 | 159-160,181-182,195-196
report.js | 76.17 | 57.89 | 80 | 76.17 | ...284,290-292,313-318,329-330
source-map-from-file.js | 45 | 100 | 0 | 45 | 39-50,52-67,69-77,81-98
source-map-from-file.js | 48.14 | 100 | 0 | 48.14 | 39-50,52-67,69-77,81-88,91-101
lib/commands | 41.44 | 66.66 | 16.66 | 41.44 |
check-coverage.js | 18.57 | 100 | 0 | 18.57 | 9-11,14-36,39-53,55-70
report.js | 80.48 | 62.5 | 50 | 80.48 | 9-10,15-20
test/fixtures | 83.33 | 85.71 | 66.66 | 83.33 |
async.js | 100 | 100 | 100 | 100 |
normal.js | 75 | 66.66 | 33.33 | 75 | 14-16,18-20
--------------------------|---------|----------|---------|---------|--------------------------------
,ERROR: Coverage for lines (74.04%) does not meet global threshold (101%)
,ERROR: Coverage for lines (74.17%) does not meet global threshold (101%)
ERROR: Coverage for branches (58.82%) does not meet global threshold (82%)
ERROR: Coverage for statements (74.04%) does not meet global threshold (95%)
ERROR: Coverage for statements (74.17%) does not meet global threshold (95%)
"
`;
Expand Down
40 changes: 39 additions & 1 deletion test/source-map-from-file.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
/* global describe, it */
const getSourceMapFromFile = require('../lib/source-map-from-file')
const { getSourceMapFromFile, sourceMapFromDataUrl } = require('../lib/source-map-from-file')
const assert = require('assert')
const { readFileSync } = require('fs')
const chia = require('chai')
const spies = require('chai-spies')

chia.use(spies)

const { expect } = chia

describe('source-map-from-file', () => {
it('should parse source maps from compiled targets', () => {
const sourceMap = getSourceMapFromFile('./test/fixtures/all/ts-compiled/main.js')
Expand All @@ -16,4 +23,35 @@ describe('source-map-from-file', () => {
const sourceMap = getSourceMapFromFile('./test/fixtures/source-maps/inline.js')
assert.strictEqual(sourceMap.version, 3)
})

it('should throw error if source map isn\'t JSON format', () => {
const spy = chia.spy
spy.on(JSON, 'parse', (content) => {
throw new Error('No actual error. Just testing code coverage. ')
})
try {
getSourceMapFromFile('./test/fixtures/all/ts-compiled/main.js')
} finally {
expect(JSON.parse).to.have.been.called()
spy.restore()
}

spy.on(JSON, 'parse', (content) => {
throw new Error('No actual error. Just testing code coverage. ')
})
try {
const mockUrl = 'application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb...'
const sourceMap = sourceMapFromDataUrl(mockUrl)
expect(sourceMap).to.equal(null)
} finally {
expect(JSON.parse).to.have.been.called()
spy.restore()
}
})

it('should throw error if url doesn\'t have a json content type', () => {
const mockUrl = 'application/text;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb...'
const sourceMap = sourceMapFromDataUrl(mockUrl)
expect(sourceMap).to.equal(null)
})
})

0 comments on commit 67ad118

Please sign in to comment.