test(science): make definition lookups actually assert - #3919
Conversation
The non-seeded science tests wrapped the definitions `.find()` calls in an arrow function passed to `expect(...).toBeTruthy()`. Since a function is always truthy, those assertions were no-ops and never validated that the returned `name`/`symbol`/`atomicNumber` actually exist in the definitions. Call `.find()` directly so each assertion checks the real lookup result. The assertions still pass because `chemicalElement()` and `unit()` draw their values from the same definitions via `arrayElement`.
✅ Deploy Preview for fakerjs ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## next #3919 +/- ##
==========================================
- Coverage 98.85% 98.79% -0.07%
==========================================
Files 923 923
Lines 3224 3224
Branches 574 591 +17
==========================================
- Hits 3187 3185 -2
- Misses 33 35 +2
Partials 4 4 🚀 New features to boost your workflow:
|
Shinigami92
left a comment
There was a problem hiding this comment.
damn, good catch 😲
we might want to inform vitest team or vitest-lint team to introduce a catching lint rule for such a case
There was a problem hiding this comment.
Pull request overview
Fixes ineffective assertions in the science module’s non-seeded tests by ensuring definition lookups actually evaluate the .find() result (instead of a truthy function reference), so regressions where returned values no longer exist in faker.definitions would now be caught.
Changes:
- Replaced
expect(() => { ...find... }).toBeTruthy()withexpect(...find...).toBeTruthy()for chemical element name, symbol, and atomic number. - Applied the same fix for unit name and unit symbol lookups.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This is definitely an error on our side. Checking if a function ref is truthy is a valid use case. Think of the following: // in *.ts file
type MathStrategy = (a: number, b: number) => number;
function getMathStrategy(strategy: string): MathStrategy | undefined {
switch(strategy) {
case 'add': return (a, b) => a + b;
case 'subtract': return (a, b) => a - b;
case 'multiply': return (a, b) => a * b;
case 'divide': return (a, b) => a / b;
default: return undefined;
}
}
// in *.spec.ts file
it('should return a valid strategy for "add"', () => {
const strategy = getMathStrategy('add');
// strategy is nothing else than a anonymous function here
// doesn't matter if it was previously assigned or not
expect(strategy).toBeTruthy();
});Just to be clear, using a strategy/factory pattern that does not return a valid or at least no-op strategy in all cases does not make sense in my mind, but it was the easiest way to write a POC. |
Context
The non-seeded tests in
test/modules/science.spec.tswere meant to verify that the values returned byfaker.science.chemicalElement()andfaker.science.unit()exist in the corresponding definitions.However, each lookup was wrapped in an arrow function passed to
expect(...).toBeTruthy():Since a function is always truthy,
expect(...).toBeTruthy()always passes regardless of whether.find()returns a match orundefined. These assertions were effectively no-ops and could not catch a regression where a returned value was no longer present in the definitions.Change
Call
.find()directly soexpect(...).toBeTruthy()checks the real lookup result instead of a function reference:Applied to all five affected assertions (element name/symbol/atomic number, unit name/symbol). No production code is touched.
The assertions still pass because
chemicalElement()andunit()draw their values from the same definitions viaarrayElement, so each lookup now genuinely succeeds.Validation
pnpm exec vitest run test/modules/science.spec.ts— 44 tests passed, no type errorspnpm exec eslint test/modules/science.spec.ts— no errorspnpm exec prettier --write test/modules/science.spec.ts— file unchanged (already formatted)