Skip to content

Commit

Permalink
fix: pass null to transform function for backwards compat
Browse files Browse the repository at this point in the history
  • Loading branch information
d-fischer committed Sep 8, 2023
1 parent f4eb2d7 commit 2b38510
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class Explorer extends ExplorerBase<InternalOptions> {
}
return await search();
}
return null;
return await this.config.transform(null);
};

if (this.searchCache) {
Expand Down
2 changes: 1 addition & 1 deletion src/ExplorerSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class ExplorerSync extends ExplorerBase<InternalOptionsSync> {
}
return search();
}
return null;
return this.config.transform(null);
};

if (this.searchCache) {
Expand Down
39 changes: 38 additions & 1 deletion test/failed-files.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { beforeEach, afterAll, describe, test, expect } from 'vitest';
import { beforeEach, afterAll, describe, test, expect, vi } from 'vitest';
import { TempDir } from './util';
import { cosmiconfig, cosmiconfigSync } from '../src';

Expand Down Expand Up @@ -209,6 +209,43 @@ describe('returns an empty config result for empty file, format YAML', () => {
});
});

describe('[#325] transforms & returns null when no config file is found', () => {
beforeEach(() => {
// temp.createFile('package.json', '{"name": "failed-files-tests"}');
});
const checkTransformResult = vi.fn((result: any) =>
expect(result).toBeNull(),
);
const checkResult = (result: any) => expect(result).toBeNull();

const startAndStopDir = temp.absolutePath('.');
test('async', async () => {
const result = await cosmiconfig('failed-files-tests', {
searchPlaces: ['package.json'],
stopDir: startAndStopDir,
async transform(innerResult) {
checkTransformResult(innerResult);
return innerResult;
},
}).search(startAndStopDir);
checkResult(result);
expect(checkTransformResult).toHaveBeenCalledTimes(1);
});

test('sync', () => {
const result = cosmiconfigSync('failed-files-tests', {
searchPlaces: ['package.json'],
stopDir: startAndStopDir,
transform(innerResult) {
checkTransformResult(innerResult);
return innerResult;
},
}).search(startAndStopDir);
checkResult(result);
expect(checkTransformResult).toHaveBeenCalledTimes(1);
});
});

describe('errors not swallowed when async custom loader throws them', () => {
const file = temp.absolutePath('.foorc.things');
beforeEach(() => {
Expand Down

0 comments on commit 2b38510

Please sign in to comment.