Skip to content

Commit

Permalink
fix(core): reset path in fn context (#2389)
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip authored Jan 31, 2023
1 parent 6bbb6cb commit 3d47ec4
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
67 changes: 67 additions & 0 deletions packages/core/src/__tests__/linter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1682,4 +1682,71 @@ responses:: !!foo
}),
]);
});

test.concurrent('should reset path provided in fn context', async () => {
const spectral = new Spectral();
const fn = jest.fn();

spectral.setRuleset({
rules: {
'valid-info': {
given: '$.info',
then: [
{
field: 'title',
function: truthy,
},
{
function: fn,
},
{
field: 'description',
function: truthy,
},
{
function: fn,
},
],
},
},
});

const documentUri = path.join(__dirname, './__fixtures__/test.json');
const document = new Document(
JSON.stringify({
info: {
title: 'test',
description: 'some description',
},
}),
Parsers.Json,
documentUri,
);

await expect(spectral.run(document)).resolves.toEqual([]);

expect(fn).nthCalledWith(
1,
{
title: 'test',
description: 'some description',
},
null,
expect.objectContaining({
path: ['info'],
}),
);

expect(fn).nthCalledWith(
2,
{
title: 'test',
description: 'some description',
},
null,
expect.objectContaining({
path: ['info'],
}),
);
});
});
2 changes: 2 additions & 0 deletions packages/core/src/runner/lintNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export const lintNode = (context: IRunnerInternalContext, node: IGivenNode, rule
for (const target of targets) {
if (target.path.length > 0) {
fnContext.path = [...givenPath, ...target.path];
} else {
fnContext.path = givenPath;
}

let targetResults;
Expand Down

0 comments on commit 3d47ec4

Please sign in to comment.