Skip to content

Commit

Permalink
Fixed test on Node 20
Browse files Browse the repository at this point in the history
refs TryGhost/gscan@f39d1d3

- similar to the commit above, the JSON parser changed between Node 18
  and Node 20, so the error message changed too
- we actually just want to check the error is forwarded to the user, so
  we can do that by getting the error message from JSON.parse and check
  against that
  • Loading branch information
daniellockyer committed Apr 18, 2024
1 parent 9e98be9 commit c40713b
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions ghost/core/test/unit/server/services/custom-redirects/api.test.js
Expand Up @@ -95,15 +95,29 @@ describe('UNIT: redirects CustomRedirectsAPI class', function () {

describe('setFromFilePath', function () {
it('throws a syntax error when setting invalid JSON redirects file', async function () {
const invalidJSON = '{invalid json';
const invalidFilePath = path.join(__dirname, '/invalid/redirects/path.json');
fs.readFile.withArgs(invalidFilePath, 'utf-8').resolves('{invalid json');
fs.readFile.withArgs(invalidFilePath, 'utf-8').resolves(invalidJSON);

let expectedErrorMessage;

try {
JSON.parse(invalidJSON);
} catch (err) {
expectedErrorMessage = err.message;
}

if (!expectedErrorMessage) {
// This should never happen because the JSON is invalid
should.fail('expectedErrorMessage is not set');
}

try {
await customRedirectsAPI.setFromFilePath(invalidFilePath, '.json');
should.fail('setFromFilePath did not throw');
} catch (err) {
should.exist(err);
err.message.should.eql('Could not parse JSON: Unexpected token i in JSON at position 1.');
err.message.should.eql(`Could not parse JSON: ${expectedErrorMessage}.`);
}
});

Expand Down

0 comments on commit c40713b

Please sign in to comment.