Skip to content

Commit

Permalink
Add tests for module import with and without cache
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilianoBruni committed Dec 25, 2024
1 parent 9bdb128 commit adce456
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
DEFAULT_EXPECTED_RESULT_FROM_SAMPLE_DIRECTORY,
DEFAULT_RELATIVE_PATH_TO_SAMPLE_DIRECTORY,
} from './constants';
import fs from 'fs';

test('Import modules from the default (current) directory synchronously', () => {
const result = directoryImport();
Expand Down Expand Up @@ -221,3 +222,44 @@ test('Import modules with specified options and call the provided callback for e
expect(result).toEqual(DEFAULT_EXPECTED_RESULT_FROM_SAMPLE_DIRECTORY);
expect(callbackResults).toEqual(DEFAULT_EXPECTED_CALLBACK_RESULTS_FROM_SAMPLE_DIRECTORY);
});

test('Import modules with cache', () => {
const result = directoryImport(DEFAULT_RELATIVE_PATH_TO_SAMPLE_DIRECTORY)

expect(result).toEqual(DEFAULT_EXPECTED_RESULT_FROM_SAMPLE_DIRECTORY);

// change the content of sample-file-2.js
fs.writeFileSync(`${DEFAULT_ABSOLUTE_PATH_TO_SAMPLE_DIRECTORY}/sample-file-2.js`, "// eslint-disable-next-line unicorn/no-empty-file, no-undef, unicorn/prefer-module\nmodule.exports = { testData: 'Hello World Changed!' };\n");

// re-import the modules
const result2 = directoryImport({
targetDirectoryPath: DEFAULT_RELATIVE_PATH_TO_SAMPLE_DIRECTORY,
forceReload: false,
});

expect(result2).toEqual(DEFAULT_EXPECTED_RESULT_FROM_SAMPLE_DIRECTORY);
// revert the content of sample-file-2.js
fs.writeFileSync(`${DEFAULT_ABSOLUTE_PATH_TO_SAMPLE_DIRECTORY}/sample-file-2.js`, "// eslint-disable-next-line unicorn/no-empty-file, no-undef, unicorn/prefer-module\nmodule.exports = { testData: 'Hello World!' };\n");
});

test('Import modules without cache', () => {
const result = directoryImport(DEFAULT_RELATIVE_PATH_TO_SAMPLE_DIRECTORY)

expect(result).toEqual(DEFAULT_EXPECTED_RESULT_FROM_SAMPLE_DIRECTORY);

// change the content of sample-file-2.js
fs.writeFileSync(`${DEFAULT_ABSOLUTE_PATH_TO_SAMPLE_DIRECTORY}/sample-file-2.js`, "// eslint-disable-next-line unicorn/no-empty-file, no-undef, unicorn/prefer-module\nmodule.exports = { testData: 'Hello World Changed!' };\n");

jest.resetModules();

// re-import the modules
const result2 = directoryImport({
targetDirectoryPath: DEFAULT_RELATIVE_PATH_TO_SAMPLE_DIRECTORY,
forceReload: true,
});

expect(result2['/sample-file-2.js']).toEqual({ testData: 'Hello World Changed!' });

// revert the content of sample-file-2.js
fs.writeFileSync(`${DEFAULT_ABSOLUTE_PATH_TO_SAMPLE_DIRECTORY}/sample-file-2.js`, "// eslint-disable-next-line unicorn/no-empty-file, no-undef, unicorn/prefer-module\nmodule.exports = { testData: 'Hello World!' };\n");
});

0 comments on commit adce456

Please sign in to comment.