Skip to content

Commit

Permalink
test(coverage): JSDOM with process.env usage
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Aug 15, 2023
1 parent 5a07cff commit fd4e773
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ exports[`custom json report 1`] = `
"<process-cwd>/src/importEnv.ts",
"<process-cwd>/src/index.mts",
"<process-cwd>/src/multi-suite.ts",
"<process-cwd>/src/process-env.ts",
"<process-cwd>/src/utils.ts",
],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1425,6 +1425,54 @@ exports[`istanbul json report 1`] = `
},
},
},
"<process-cwd>/src/process-env.ts": {
"b": {},
"branchMap": {},
"f": {
"0": 1,
},
"fnMap": {
"0": {
"decl": {
"end": {
"column": 29,
"line": 1,
},
"start": {
"column": 16,
"line": 1,
},
},
"loc": {
"end": {
"column": null,
"line": 3,
},
"start": {
"column": 29,
"line": 1,
},
},
"name": "getNodeEnv",
},
},
"path": "<process-cwd>/src/process-env.ts",
"s": {
"0": 1,
},
"statementMap": {
"0": {
"end": {
"column": null,
"line": 2,
},
"start": {
"column": 2,
"line": 2,
},
},
},
},
"<process-cwd>/src/untested-file.ts": {
"b": {
"0": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3528,6 +3528,109 @@ exports[`v8 json report 1`] = `
},
},
},
"<process-cwd>/src/process-env.ts": {
"all": false,
"b": {
"0": [
1,
],
},
"branchMap": {
"0": {
"line": 1,
"loc": {
"end": {
"column": 1,
"line": 3,
},
"start": {
"column": 7,
"line": 1,
},
},
"locations": [
{
"end": {
"column": 1,
"line": 3,
},
"start": {
"column": 7,
"line": 1,
},
},
],
"type": "branch",
},
},
"f": {
"0": 1,
},
"fnMap": {
"0": {
"decl": {
"end": {
"column": 1,
"line": 3,
},
"start": {
"column": 7,
"line": 1,
},
},
"line": 1,
"loc": {
"end": {
"column": 1,
"line": 3,
},
"start": {
"column": 7,
"line": 1,
},
},
"name": "getNodeEnv",
},
},
"path": "<process-cwd>/src/process-env.ts",
"s": {
"0": 1,
"1": 1,
"2": 1,
},
"statementMap": {
"0": {
"end": {
"column": 30,
"line": 1,
},
"start": {
"column": 0,
"line": 1,
},
},
"1": {
"end": {
"column": 29,
"line": 2,
},
"start": {
"column": 0,
"line": 2,
},
},
"2": {
"end": {
"column": 1,
"line": 3,
},
"start": {
"column": 0,
"line": 3,
},
},
},
},
"<process-cwd>/src/untested-file.ts": {
"all": true,
"b": {
Expand Down
10 changes: 10 additions & 0 deletions test/coverage-test/coverage-report-tests/istanbul.report.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Istanbul coverage provider specific test cases
*/

import { resolve } from 'node:path'
import { readFileSync } from 'node:fs'
import { expect, test } from 'vitest'
import { readCoverageJson } from './utils'

Expand Down Expand Up @@ -50,3 +52,11 @@ test('tests with multiple suites are covered', async () => {
1: 1,
})
})

test('vite transforms should not show on coverage report', async () => {
const filePath = resolve('./coverage/src/process-env.ts.html')
const htmlContent = readFileSync(filePath, 'utf-8')

// Actual source code
expect(htmlContent).toContain('process.env.NODE_ENV')
})
3 changes: 3 additions & 0 deletions test/coverage-test/src/process-env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function getNodeEnv() {
return process.env.NODE_ENV
}
8 changes: 8 additions & 0 deletions test/coverage-test/test/jsdom.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @vitest-environment jsdom

import { expect, test } from 'vitest'
import { getNodeEnv } from '../src/process-env'

test('process.env works', () => {
expect(getNodeEnv()).toBe('test')
})

0 comments on commit fd4e773

Please sign in to comment.