Skip to content

Commit

Permalink
Updated to use assert/strict everywhere (TryGhost#17047)
Browse files Browse the repository at this point in the history
refs: https://github.com/TryGhost/Toolbox/issues/595

We're rolling out new rules around the node assert library, the first of which is enforcing the use of assert/strict. This means we don't need to use the strict version of methods, as the standard version will work that way by default.

This caught some gotchas in our existing usage of assert where the lack of strict mode had unexpected results:
- Url matching needs to be done on `url.href` see TryGhost@aa58b35
- Null and undefined are not the same thing,  there were a few cases of this being confused
- Particularly questionable changes in [PostExporter tests](TryGhost@c1a4687) tracked [here](TryGhost/Product#3505).
- A typo see TryGhost@eaac9c2

Moving forward, using assert strict should help us to catch unexpected behaviour, particularly around nulls and undefineds during implementation.
  • Loading branch information
ErisDS authored Jun 21, 2023
1 parent 3e93245 commit 6161f94
Show file tree
Hide file tree
Showing 160 changed files with 262 additions and 260 deletions.
2 changes: 1 addition & 1 deletion apps/signup-form/test/unit/hello.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert');
const assert = require('assert/strict');

describe('Hello world', function () {
it('Runs a test', function () {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert');
const assert = require('assert/strict');
const MemoryTTLCache = require('../index');

const sleep = ms => (
Expand Down
2 changes: 1 addition & 1 deletion ghost/adapter-cache-redis/test/adapter-cache-redis.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert');
const assert = require('assert/strict');
const sinon = require('sinon');
const RedisCache = require('../index');

Expand Down
2 changes: 1 addition & 1 deletion ghost/admin-x-settings/test/hello.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert');
const assert = require('assert/strict');

describe('Hello world', function () {
it('Runs a test', function () {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert');
const assert = require('assert/strict');
const AnnouncementBarSettings = require('../index');

describe('AnnouncementBarSettings', function () {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert');
const assert = require('assert/strict');
const sinon = require('sinon');
const APIVersionCompatibilityService = require('../index');

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert');
const assert = require('assert/strict');
const {AudienceFeedbackService} = require('../index');

describe('audienceFeedbackService', function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = (url) => {

let {version, api} = pathMatch(apiRouteMatcher)(urlToMatch);

if (version === [null]) {
if (version === undefined) {
version = null;
}

Expand Down
2 changes: 1 addition & 1 deletion ghost/core/test/e2e-api/admin/activity-feed.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const {agentProvider, mockManager, fixtureManager, matchers} = require('../../utils/e2e-framework');
const {anyEtag, anyErrorId, anyObjectId, anyContentLength, anyContentVersion, anyUuid, anyISODate, anyString, anyObject, anyNumber} = matchers;

const assert = require('assert');
const assert = require('assert/strict');
const moment = require('moment');
const sinon = require('sinon');
const logging = require('@tryghost/logging');
Expand Down
2 changes: 1 addition & 1 deletion ghost/core/test/e2e-api/admin/collections.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert');
const assert = require('assert/strict');
const {
agentProvider,
fixtureManager,
Expand Down
2 changes: 1 addition & 1 deletion ghost/core/test/e2e-api/admin/email-previews.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const {agentProvider, fixtureManager, matchers, mockManager} = require('../../utils/e2e-framework');
const {anyEtag, anyErrorId, anyContentVersion, anyString} = matchers;
const assert = require('assert');
const assert = require('assert/strict');
const sinon = require('sinon');
const escapeRegExp = require('lodash/escapeRegExp');
const should = require('should');
Expand Down
2 changes: 1 addition & 1 deletion ghost/core/test/e2e-api/admin/emails.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const {agentProvider, fixtureManager, matchers, mockManager} = require('../../utils/e2e-framework');
const {nullable, anyContentVersion, anyEtag, anyObjectId, anyUuid, anyISODateTime, anyString} = matchers;
const assert = require('assert');
const assert = require('assert/strict');
const sinon = require('sinon');
const jobManager = require('../../../core/server/services/jobs/job-service');
const models = require('../../../core/server/models');
Expand Down
4 changes: 2 additions & 2 deletions ghost/core/test/e2e-api/admin/images.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const {agentProvider, fixtureManager, matchers} = require('../../utils/e2e-frame
const FormData = require('form-data');
const p = require('path');
const {promises: fs} = require('fs');
const assert = require('assert');
const assert = require('assert/strict');
const config = require('../../../core/shared/config');
const urlUtils = require('../../../core/shared/url-utils');
const imageTransform = require('@tryghost/image-transform');
Expand Down Expand Up @@ -59,7 +59,7 @@ const uploadImageCheck = async ({path, filename, contentType, expectedFileName,
expectedFileName = expectedFileName || filename;

assert.match(body.images[0].url, new RegExp(`${urlUtils.urlFor('home', true)}content/images/\\d+/\\d+/${expectedFileName}`));
assert.equal(body.images[0].ref, ref);
assert.equal(body.images[0].ref, ref === undefined ? null : ref);

const relativePath = body.images[0].url.replace(urlUtils.urlFor('home', true), '/');
const filePath = config.getContentPath('images') + relativePath.replace('/content/images/', '');
Expand Down
2 changes: 1 addition & 1 deletion ghost/core/test/e2e-api/admin/members.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const {anyContentVersion, anyEtag, anyObjectId, anyUuid, anyISODateTime, anyISOD
const {queryStringToken} = regexes;
const ObjectId = require('bson-objectid').default;

const assert = require('assert');
const assert = require('assert/strict');
const nock = require('nock');
const sinon = require('sinon');
const should = require('should');
Expand Down
2 changes: 1 addition & 1 deletion ghost/core/test/e2e-api/admin/newsletters.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert');
const assert = require('assert/strict');
const sinon = require('sinon');
const {agentProvider, mockManager, fixtureManager, configUtils, dbUtils, matchers, regexes} = require('../../utils/e2e-framework');
const {anyContentVersion, anyEtag, anyObjectId, anyUuid, anyISODateTime, anyLocationFor, anyNumber} = matchers;
Expand Down
2 changes: 1 addition & 1 deletion ghost/core/test/e2e-api/admin/pages-bulk.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const {agentProvider, fixtureManager, mockManager} = require('../../utils/e2e-framework');
const models = require('../../../core/server/models');
const assert = require('assert');
const assert = require('assert/strict');

const forcePageFilter = (filter) => {
if (filter) {
Expand Down
2 changes: 1 addition & 1 deletion ghost/core/test/e2e-api/admin/posts-bulk.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const {agentProvider, fixtureManager, mockManager} = require('../../utils/e2e-framework');
const models = require('../../../core/server/models');
const assert = require('assert');
const assert = require('assert/strict');

describe('Posts Bulk API', function () {
let agent;
Expand Down
10 changes: 5 additions & 5 deletions ghost/core/test/e2e-api/admin/settings.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert');
const assert = require('assert/strict');
const sinon = require('sinon');
const logging = require('@tryghost/logging');
const SingleUseTokenProvider = require('../../../core/server/services/members/SingleUseTokenProvider');
Expand Down Expand Up @@ -248,7 +248,7 @@ describe('Settings API', function () {
})
.expect(({body}) => {
const emailVerificationRequired = body.settings.find(setting => setting.key === 'email_verification_required');
assert.strictEqual(emailVerificationRequired.value, false);
assert.equal(emailVerificationRequired.value, false);
});
mockManager.assert.sentEmailCount(0);
});
Expand All @@ -270,7 +270,7 @@ describe('Settings API', function () {
})
.expect(({body}) => {
const membersSupportAddress = body.settings.find(setting => setting.key === 'members_support_address');
assert.strictEqual(membersSupportAddress.value, 'noreply');
assert.equal(membersSupportAddress.value, 'noreply');

assert.deepEqual(body.meta, {
sent_email_verification: ['members_support_address']
Expand Down Expand Up @@ -306,7 +306,7 @@ describe('Settings API', function () {
})
.expect(({body}) => {
const membersSupportAddress = body.settings.find(setting => setting.key === 'members_support_address');
assert.strictEqual(membersSupportAddress.value, '[email protected]');
assert.equal(membersSupportAddress.value, '[email protected]');

assert.deepEqual(body.meta, {});
});
Expand Down Expand Up @@ -397,7 +397,7 @@ describe('Settings API', function () {
})
.expect(({body}) => {
const membersSupportAddress = body.settings.find(setting => setting.key === 'members_support_address');
assert.strictEqual(membersSupportAddress.value, '[email protected]');
assert.equal(membersSupportAddress.value, '[email protected]');
});

mockManager.assert.sentEmailCount(0);
Expand Down
10 changes: 5 additions & 5 deletions ghost/core/test/e2e-api/admin/stats.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const {agentProvider, fixtureManager, matchers, mockManager} = require('../../utils/e2e-framework');
const {mockStripe, stripeMocker} = require('../../utils/e2e-framework-mock-manager');
const {anyContentVersion, anyEtag, anyISODate, anyObjectId} = matchers;
const assert = require('assert');
const assert = require('assert/strict');

let agent;

Expand Down Expand Up @@ -104,7 +104,7 @@ describe('Stats API', function () {
etag: anyEtag
})
.expect(({body}) => {
assert.deepStrictEqual(body, before, 'A free trial should not be counted as a paid subscriber');
assert.deepEqual(body, before, 'A free trial should not be counted as a paid subscriber');
});

// Activate the subscription
Expand All @@ -124,7 +124,7 @@ describe('Stats API', function () {
etag: anyEtag
})
.expect(({body}) => {
assert.notDeepStrictEqual(body, before, 'The stats should change after a free trial is activated');
assert.notDeepEqual(body, before, 'The stats should change after a free trial is activated');
});
});

Expand All @@ -151,7 +151,7 @@ describe('Stats API', function () {
etag: anyEtag
})
.expect(({body}) => {
assert.deepStrictEqual(body, before, 'An incomplete subscription should not be counted as a paid subscriber');
assert.deepEqual(body, before, 'An incomplete subscription should not be counted as a paid subscriber');
});

// Activate the subscription
Expand All @@ -170,7 +170,7 @@ describe('Stats API', function () {
etag: anyEtag
})
.expect(({body}) => {
assert.notDeepStrictEqual(body, before, 'The stats should change after an incomplete subscription is activated');
assert.notDeepEqual(body, before, 'The stats should change after an incomplete subscription is activated');
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion ghost/core/test/e2e-api/admin/tiers.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert');
const assert = require('assert/strict');
const {
agentProvider,
fixtureManager,
Expand Down
2 changes: 1 addition & 1 deletion ghost/core/test/e2e-api/content/pages.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert');
const assert = require('assert/strict');
const moment = require('moment');

const testUtils = require('../../utils');
Expand Down
5 changes: 3 additions & 2 deletions ghost/core/test/e2e-api/content/posts.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert');
const assert = require('assert/strict');
const cheerio = require('cheerio');
const moment = require('moment');
const testUtils = require('../../utils');
Expand Down Expand Up @@ -111,7 +111,8 @@ describe('Posts Content API', function () {
} else {
const tag = post.tags
.map(t => t.slug)
.filter(s => s === 'kitchen-sink');
.filter(s => s === 'kitchen-sink')
.pop();
assert.equal(tag, 'kitchen-sink', `Each post must either be featured or have the tag 'kitchen-sink'`);
}
});
Expand Down
2 changes: 1 addition & 1 deletion ghost/core/test/e2e-api/content/tags.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert');
const assert = require('assert/strict');
const should = require('should');
const supertest = require('supertest');
const _ = require('lodash');
Expand Down
2 changes: 1 addition & 1 deletion ghost/core/test/e2e-api/members-comments/comments.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert');
const assert = require('assert/strict');
const {agentProvider, mockManager, fixtureManager, matchers, configUtils} = require('../../utils/e2e-framework');
const {anyEtag, anyObjectId, anyLocationFor, anyISODateTime, anyErrorId, anyUuid, anyNumber, anyBoolean} = matchers;
const should = require('should');
Expand Down
2 changes: 1 addition & 1 deletion ghost/core/test/e2e-api/members/feedback.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert');
const assert = require('assert/strict');
const {agentProvider, mockManager, fixtureManager, matchers, configUtils} = require('../../utils/e2e-framework');
const {anyEtag, anyObjectId, anyLocationFor, anyErrorId} = matchers;
const models = require('../../../core/server/models');
Expand Down
2 changes: 1 addition & 1 deletion ghost/core/test/e2e-api/members/signin.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const {agentProvider, mockManager, fixtureManager, configUtils, resetRateLimits, dbUtils} = require('../../utils/e2e-framework');
const models = require('../../../core/server/models');
const assert = require('assert');
const assert = require('assert/strict');
require('should');
const sinon = require('sinon');

Expand Down
2 changes: 1 addition & 1 deletion ghost/core/test/e2e-api/members/webhooks.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const crypto = require('crypto');
const assert = require('assert');
const assert = require('assert/strict');
const nock = require('nock');
const should = require('should');
const stripe = require('stripe');
Expand Down
2 changes: 1 addition & 1 deletion ghost/core/test/e2e-api/webmentions/webmentions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const {
configUtils
} = require('../../utils/e2e-framework');
const models = require('../../../core/server/models');
const assert = require('assert');
const assert = require('assert/strict');
const urlUtils = require('../../../core/shared/url-utils');
const nock = require('nock');
const jobsService = require('../../../core/server/services/mentions-jobs');
Expand Down
2 changes: 1 addition & 1 deletion ghost/core/test/e2e-frontend/members.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert');
const assert = require('assert/strict');
const should = require('should');
const sinon = require('sinon');
const supertest = require('supertest');
Expand Down
2 changes: 1 addition & 1 deletion ghost/core/test/e2e-server/1-options-requests.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert');
const assert = require('assert/strict');
const {agentProvider, matchers} = require('../utils/e2e-framework');
const {anyContentVersion} = matchers;
const config = require('../../core/shared/config');
Expand Down
2 changes: 1 addition & 1 deletion ghost/core/test/e2e-server/click-tracking.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert');
const assert = require('assert/strict');
const fetch = require('node-fetch').default;
const {agentProvider, mockManager, fixtureManager} = require('../utils/e2e-framework');
const urlUtils = require('../../core/shared/url-utils');
Expand Down
2 changes: 1 addition & 1 deletion ghost/core/test/e2e-server/jobs/update-check.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert');
const assert = require('assert/strict');
const http = require('http');
const path = require('path');

Expand Down
2 changes: 1 addition & 1 deletion ghost/core/test/e2e-server/services/mentions.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const {agentProvider, fixtureManager, mockManager} = require('../../utils/e2e-framework');
const nock = require('nock');
const assert = require('assert');
const assert = require('assert/strict');
const markdownToMobiledoc = require('../../utils/fixtures/data-generator').markdownToMobiledoc;
const jobsService = require('../../../core/server/services/mentions-jobs');

Expand Down
2 changes: 1 addition & 1 deletion ghost/core/test/e2e-server/services/milestones.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const {agentProvider, fixtureManager, mockManager, configUtils} = require('../../utils/e2e-framework');
const assert = require('assert');
const assert = require('assert/strict');
const nock = require('nock');
const sinon = require('sinon');
const models = require('../../../core/server/models');
Expand Down
2 changes: 1 addition & 1 deletion ghost/core/test/integration/importer/v2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const testUtils = require('../../utils');
const Promise = require('bluebird');
const moment = require('moment-timezone');
const ObjectId = require('bson-objectid').default;
const assert = require('assert');
const assert = require('assert/strict');
const _ = require('lodash');
const validator = require('@tryghost/validator');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const moment = require('moment');
const ObjectId = require('bson-objectid').default;
const models = require('../../../../core/server/models');
const sinon = require('sinon');
const assert = require('assert');
const assert = require('assert/strict');
const jobManager = require('../../../../core/server/services/jobs/job-service');
const _ = require('lodash');
const {MailgunEmailProvider} = require('@tryghost/email-service');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const sinon = require('sinon');
const {agentProvider, fixtureManager} = require('../../../utils/e2e-framework');
const assert = require('assert');
const assert = require('assert/strict');
const MailgunClient = require('@tryghost/mailgun-client');
const DomainEvents = require('@tryghost/domain-events');
const emailAnalytics = require('../../../../core/server/services/email-analytics');
Expand Down Expand Up @@ -462,7 +462,7 @@ describe('EmailEventStorage', function () {
// Message and code not changed
assert.equal(permanentFailures.models[0].get('message'), 'Not delivering to previously bounced address');
assert.equal(permanentFailures.models[0].get('code'), 605);
assert.equal(permanentFailures.models[0].get('enhanded_code'), null);
assert.equal(permanentFailures.models[0].get('enhanced_code'), null);
assert.notEqual(permanentFailures.models[0].get('failed_at').toUTCString(), timestamp.toUTCString());
});

Expand Down Expand Up @@ -997,7 +997,7 @@ describe('EmailEventStorage', function () {
// Check not unsubscribed
const {body: {events: eventsBefore}} = await agent.get(eventsURI);
const existingSpamEvent = eventsBefore.find(event => event.type === 'email_complaint_event');
assert.equal(existingSpamEvent, null, 'This test requires a member that does not have a spam event');
assert.equal(existingSpamEvent, undefined, 'This test requires a member that does not have a spam event');

events = [{
event: 'complained',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require('should');
const {agentProvider, fixtureManager, mockManager} = require('../../utils/e2e-framework');
const models = require('../../../core/server/models');
const assert = require('assert');
const assert = require('assert/strict');
let agent;

describe('Last Seen At Updater', function () {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const sinon = require('sinon');
const {agentProvider, fixtureManager} = require('../../utils/e2e-framework');
const assert = require('assert');
const assert = require('assert/strict');
const MailgunClient = require('@tryghost/mailgun-client');
const DomainEvents = require('@tryghost/domain-events');
const emailAnalytics = require('../../../core/server/services/email-analytics');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const sinon = require('sinon');
const {agentProvider, fixtureManager} = require('../../../utils/e2e-framework');
const assert = require('assert');
const assert = require('assert/strict');
const models = require('../../../../core/server/models');

describe('Job: Clean tokens', function () {
Expand Down
Loading

0 comments on commit 6161f94

Please sign in to comment.