Experimental Test Coverage throws "cannot read properties of undefined (reading: line)" #52775
Description
Version
22.0.0
Platform
Darwin Turing-2.local 23.4.0 Darwin Kernel Version 23.4.0: Fri Mar 15 00:10:42 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6000 arm64
Subsystem
Test Runner
What steps will reproduce the bug?
While running this command: NODE_ENV='test' tsx --test --experimental-test-coverage './src/**/*.test.ts' (same thing happens with
NODE_ENV='test' node --import='tsx/esm' --test --experimental-test-coverage './src/**/*.test.ts'`) in this file:
import assert from 'node:assert'
import { readFileSync, readdirSync, unlinkSync } from 'node:fs'
import { dirname, resolve } from 'node:path'
import { before, describe, it } from 'node:test'
import { fileURLToPath } from 'node:url'
import { Teacher } from '../domain/Teacher.js'
import { TeacherRepository } from './TeacherRepository.js'
describe('TeacherRepository', () => {
const dbPath = resolve(dirname(fileURLToPath(import.meta.url)), '.data')
const teacher = new Teacher({
firstName: 'Lucas',
surname: 'Santos',
phone: '123456789',
email: 'foo@gmail.com',
document: '123456789',
hiringDate: new Date('2020-10-20').toISOString(),
major: 'Computer Science',
salary: 5000
})
before(() => {
// limpa o arquivo de teste antes de começar
// isso garante que sempre vamos ter um único registro
unlinkSync(resolve(dbPath, 'teacher-test.json'))
})
it('should create a new teachers.json file under .data', () => {
void new TeacherRepository()
const dirs = readdirSync(dbPath)
assert.ok(dirs.includes('teacher-test.json'))
})
it('should save a new Teacher in the database', () => {
const db = new TeacherRepository()
const instance = db.save(teacher)
assert.ok(instance instanceof TeacherRepository)
const teachersFile = JSON.parse(readFileSync(`${dbPath}/teacher-test.json`, 'utf-8'))
assert.deepStrictEqual(Teacher.fromObject(teachersFile[0][1]), teacher)
})
it('should list all teachers in the database', () => {
const db = new TeacherRepository()
const teachers = db.list() as Teacher[]
assert.ok(teachers.length === 1)
assert.ok(teachers[0] instanceof Teacher)
})
it('should find a teacher by id', () => {
const db = new TeacherRepository()
const found = db.findById(teacher.id)
assert.ok(found instanceof Teacher)
assert.deepStrictEqual(found, teacher)
})
it('should update a teacher', () => {
const db = new TeacherRepository()
teacher.firstName = 'Not Lucas'
const updated = db.save(teacher).findById(teacher.id)
assert.ok(updated instanceof Teacher)
assert.deepStrictEqual(updated, teacher)
})
it('should list teachers by a specific property', () => {
const db = new TeacherRepository()
const teachers = db.listBy('firstName', 'Not Lucas') as Teacher[]
assert.ok(teachers.length === 1)
assert.ok(teachers[0] instanceof Teacher)
assert.deepStrictEqual(teachers[0], teacher)
})
})
Shows me the result of the test runner, but the coverage is not shown:
▶ TeacherRepository
✔ should create a new teachers.json file under .data (1.419416ms)
✔ should save a new Teacher in the database (4.545542ms)
✔ should list all teachers in the database (0.264125ms)
✔ should find a teacher by id (0.181667ms)
✔ should update a teacher (0.311292ms)
✔ should list teachers by a specific property (0.801084ms)
▶ TeacherRepository (8.870708ms)
ℹ Warning: Could not report code coverage. TypeError: Cannot read properties of undefined (reading 'line')
ℹ tests 6
ℹ suites 1
ℹ pass 6
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 221.09275
How often does it reproduce? Is there a required condition?
Seems to be intermittent, I have ran files without this issue in the past but for some file it does seem to happen, however I do not know which conditions are required for the bug to be reproduced as it seemed random.
What is the expected behavior? Why is that the expected behavior?
The expected behavior is that the code coverage is shown.
What do you see instead?
Warning: Could not report code coverage. TypeError: Cannot read properties of undefined (reading 'line')
Additional information
I had this issue before in a few different repositories, but they didn't have any configurations in common, as of now the reproduction of this error eludes me.
I've checked #51552 and #51392 but it doesn't seem to be related to source maps in my case.