Code Smell 259 – Testing with External Resources

Don’t rely on things that can change without you noticing them

TL;DR: Tests must be in full control.

Problems

Unreliable tests
Difficult debugging
Unexpected changes
Coupling to External dependencies
Mistery Guests
Flaky tests
Slowness


This content originally appeared on DEV Community and was authored by Maxi Contieri

Don't rely on things that can change without you noticing them

TL;DR: Tests must be in full control.

Problems

Solutions

  1. Generate the file in the test

  2. Mock it

  3. Use hardcoded streams instead

Context

Using an existing file for testing breaks the golden rule: tests must fully control their environment.

When someone changes the file, the test becomes erratic and unreliable.

That can result in an annoying debugging experience and non-deterministic test runs.

Sample Code

Wrong

const fs = require('fs');

function trimFile(data) {
    return data.trim();
}

// existing_file.txt holds the sample information
// "     John Wick    "

test('test process file', () => {
    const data = fs.readFileSync('existing_file.txt', 'utf8');
    expect(trimFile(data)).toBe('John Wick');
});

Right

const fs = require('fs');
const { jest } = require('@jest/globals');

function trimFile(data) {
    return data.trim();
}

function generateTestData() {
    return ' John Wick ';
}

test('test process file generated', () => {
    const data = generateTestData();
    expect(trimFile(data)).toBe('John Wick');
});

test('test process file mocked', () => {
    jest.spyOn(fs, 'readFileSync').mockReturnValue(' mocked data ');
    const data = fs.readFileSync('file.txt', 'utf8');
    expect(trimFile(data)).toBe('John Wick');
    fs.readFileSync.mockRestore();
});

Detection

[X] Automatic

You can detect this smell by identifying tests that rely on external files instead of generating or mocking the data.

Look for file path references and check if they are necessary.

Tags

  • Testing

Level

[X] Intermediate

AI Generation

AI generators might create this smell if they aren't given clear instructions to generate or mock test data instead of using external files.

AI Detection

GPT tools detect this smell if you guide them to check the code for external file dependencies.

Conclusion

Never use existing files and keep your tests runnable to a known state.

You need to generate your test data either by the test or mock it out completely so that tests are in full control.

Relations

More Info

Disclaimer

Code Smells are my opinion.

Credits

Photo by William Warby on Unsplash

Code without tests is broken by design.

Jacob Kaplan-Moss

This article is part of the CodeSmell Series.


This content originally appeared on DEV Community and was authored by Maxi Contieri


Print Share Comment Cite Upload Translate Updates
APA

Maxi Contieri | Sciencx (2024-07-18T01:40:10+00:00) Code Smell 259 – Testing with External Resources. Retrieved from https://www.scien.cx/2024/07/18/code-smell-259-testing-with-external-resources/

MLA
" » Code Smell 259 – Testing with External Resources." Maxi Contieri | Sciencx - Thursday July 18, 2024, https://www.scien.cx/2024/07/18/code-smell-259-testing-with-external-resources/
HARVARD
Maxi Contieri | Sciencx Thursday July 18, 2024 » Code Smell 259 – Testing with External Resources., viewed ,<https://www.scien.cx/2024/07/18/code-smell-259-testing-with-external-resources/>
VANCOUVER
Maxi Contieri | Sciencx - » Code Smell 259 – Testing with External Resources. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/18/code-smell-259-testing-with-external-resources/
CHICAGO
" » Code Smell 259 – Testing with External Resources." Maxi Contieri | Sciencx - Accessed . https://www.scien.cx/2024/07/18/code-smell-259-testing-with-external-resources/
IEEE
" » Code Smell 259 – Testing with External Resources." Maxi Contieri | Sciencx [Online]. Available: https://www.scien.cx/2024/07/18/code-smell-259-testing-with-external-resources/. [Accessed: ]
rf:citation
» Code Smell 259 – Testing with External Resources | Maxi Contieri | Sciencx | https://www.scien.cx/2024/07/18/code-smell-259-testing-with-external-resources/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.