Skip to content

Commit

Permalink
Merge 42a40f4 into 177693c
Browse files Browse the repository at this point in the history
  • Loading branch information
ztsai committed Nov 2, 2020
2 parents 177693c + 42a40f4 commit e23bd1f
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 87 deletions.
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"seed": "cd test/rumors-db && npm run seed",
"pretest": "npm run rumors-db:test",
"test": "NODE_ENV=test ELASTICSEARCH_URL=http://localhost:62223 jest --runInBand",
"start": "pm2 startOrRestart ecosystem.config.js --env production --no-daemon",
"posttest": "NODE_ENV=test ELASTICSEARCH_URL=http://localhost:62223 babel-node test/postTest.js",
"start": "pm2 startOrRestart ecosystem.config.js --env production --no-daemon",
"lint": "eslint src/.",
"lint:fix": "eslint --fix src/.",
"rumors-db:pull": "cd test/rumors-db && git pull",
Expand Down Expand Up @@ -83,7 +84,9 @@
],
"setupFilesAfterEnv": [
"./test/setup.js"
]
],
"testSequencer": "./test/testSequencer.js"

},
"engines": {
"node": ">=12"
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import client from 'util/client';
const FIXED_DATE = 612921600000;

describe('verifyProfile', () => {
beforeAll(async () => loadFixtures(fixtures));
afterAll(async () => unloadFixtures(fixtures));
beforeAll(() => loadFixtures(fixtures));
afterAll(() => unloadFixtures(fixtures));

it('authenticates user via profile ID', async () => {
const passportProfile = {
Expand Down
4 changes: 2 additions & 2 deletions src/graphql/dataLoaders/__tests__/analyticsLoaderFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const loader = new DataLoaders();
MockDate.set(1578589200000); // 2020-01-10T01:00:00.000+08:00

describe('analyticsLoaderFactory', () => {
beforeAll(async () => await loadFixtures(fixtures));
beforeAll(() => loadFixtures(fixtures));

it('should load last 31 days of data for given id', async () => {
const res = await loader.analyticsLoader.load({
Expand Down Expand Up @@ -64,5 +64,5 @@ describe('analyticsLoaderFactory', () => {
expect(error).toBe('docType is required');
});

afterAll(async () => await unloadFixtures(fixtures));
afterAll(() => unloadFixtures(fixtures));
});
4 changes: 2 additions & 2 deletions src/graphql/models/__tests__/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ jest.mock('lodash', () => ({
}));

describe('User model', () => {
beforeAll(async () => await loadFixtures(fixtures));
afterAll(async () => await unloadFixtures(fixtures));
beforeAll(() => loadFixtures(fixtures));
afterAll(() => unloadFixtures(fixtures));

describe('currentUserOnlyField', () => {
const user = {
Expand Down
22 changes: 4 additions & 18 deletions src/graphql/mutations/__tests__/CreateOrUpdateUser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { loadFixtures, saveStateForIndices, clearIndices } from 'util/fixtures';
import { loadFixtures, unloadFixtures } from 'util/fixtures';
import client from 'util/client';
import MockDate from 'mockdate';
import fixtures from '../__fixtures__/CreateOrUpdateUser';
Expand All @@ -19,25 +19,10 @@ jest.mock('../../models/User', () => {
};
});

jest.mock('../../../rollbarInstance', () => ({
__esModule: true,
default: { error: jest.fn() },
}));

let dbStates = {};

describe('CreateOrUpdateUser', () => {
beforeAll(async () => {
dbStates = await saveStateForIndices(['users']);
await clearIndices(['users']);
await loadFixtures(fixtures);
});
beforeAll(() => loadFixtures(fixtures));

afterAll(async () => {
await clearIndices(['users']);
// restore db states to prevent affecting other tests
await loadFixtures(dbStates);
});
afterAll(() => unloadFixtures(fixtures));

it('creates backend user if not existed', async () => {
MockDate.set(1602288000000);
Expand Down Expand Up @@ -66,6 +51,7 @@ describe('CreateOrUpdateUser', () => {
rollbar.error.mockClear();

MockDate.reset();
await client.delete({ index: 'users', type: 'doc', id });
});

it("updates backend users' last active time if user already existed", async () => {
Expand Down
4 changes: 4 additions & 0 deletions src/graphql/mutations/__tests__/CreateReply.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ describe('CreateReply', () => {
type: 'doc',
id: replyId,
});

expect(reply._source).toMatchSnapshot();

// Cleanup
await client.delete({ index: 'replies', type: 'doc', id: replyId });
});

it('should throw error since a reference is required for type !== NOT_ARTICLE', async () => {
Expand Down
17 changes: 3 additions & 14 deletions src/graphql/queries/__tests__/ListReplies.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import { loadFixtures, clearIndices, saveStateForIndices } from 'util/fixtures';
import { loadFixtures, unloadFixtures } from 'util/fixtures';
import gql from 'util/GraphQL';
import { getCursor } from 'graphql/util';
import fixtures from '../__fixtures__/ListReplies';

const indices = ['replies', 'urls'];
let dbStates;
describe('ListReplies', () => {
beforeAll(async () => {
// storing the current db states to restore to after the test is completed
dbStates = await saveStateForIndices(indices);
await clearIndices(indices);
await loadFixtures(fixtures);
});
beforeAll(() => loadFixtures(fixtures));

it('lists all replies', async () => {
expect(
Expand Down Expand Up @@ -390,9 +383,5 @@ describe('ListReplies', () => {
).toMatchSnapshot();
});

afterAll(async () => {
await clearIndices(indices);
// restore db states to prevent affecting other tests
await loadFixtures(dbStates);
});
afterAll(() => unloadFixtures(fixtures));
});
12 changes: 6 additions & 6 deletions src/scripts/__tests__/fetchStatsFromGA.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('fetchStatsFromGA', () => {
yargs.argvMock.mockReset();
});

it('without any arugments', async () => {
it('without any arguments', async () => {
yargs.argvMock.mockReturnValue({
useContentGroup: true,
loadScript: false,
Expand All @@ -73,7 +73,7 @@ describe('fetchStatsFromGA', () => {
expect(storeScriptInDBMock).not.toHaveBeenCalled();
});

it('with date arugments', async () => {
it('with date arguments', async () => {
yargs.argvMock.mockReturnValue({
startDate: '2020-01-01',
endDate: '2020-02-01',
Expand All @@ -93,7 +93,7 @@ describe('fetchStatsFromGA', () => {
expect(storeScriptInDBMock).not.toHaveBeenCalled();
});

it('with loadScript arugments', async () => {
it('with loadScript arguments', async () => {
yargs.argvMock.mockReturnValue({
loadScript: true,
useContentGroup: true,
Expand All @@ -105,7 +105,7 @@ describe('fetchStatsFromGA', () => {
expect(storeScriptInDBMock).toHaveBeenCalled();
});

it('with loadScript and date arugments', async () => {
it('with loadScript and date arguments', async () => {
yargs.argvMock.mockReturnValue({
loadScript: true,
startDate: '2020-01-01',
Expand Down Expand Up @@ -474,8 +474,8 @@ describe('fetchStatsFromGA', () => {
upsertDocStatsSpy.mockClear();
});

afterAll(async () =>
await client.delete_script({ id: fetchStatsFromGA.upsertScriptID }));
afterAll(() =>
client.delete_script({ id: fetchStatsFromGA.upsertScriptID }));

it('should aggregate rows of data', async () => {
await fetchStatsFromGA.processReport(
Expand Down
20 changes: 12 additions & 8 deletions src/scripts/migrations/__tests__/createBackendUsers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { loadFixtures, clearIndices, saveStateForIndices } from 'util/fixtures';
import { loadFixtures } from 'util/fixtures';
import client from 'util/client';
import CreateBackendUsers from '../createBackendUsers';
import fixtures from '../__fixtures__/createBackendUsers';
Expand Down Expand Up @@ -39,12 +39,8 @@ const indices = [
'analytics',
];

let dbStates = {};
describe('createBackendUsers', () => {
beforeAll(async () => {
// storing the current db states to restore to after the test is completed
dbStates = await saveStateForIndices(indices);
await clearIndices(indices);
await loadFixtures(fixtures.fixturesToLoad);

await new CreateBackendUsers({
Expand All @@ -60,9 +56,17 @@ describe('createBackendUsers', () => {
});

afterAll(async () => {
await clearIndices(indices);
// restore db states to prevent affecting other tests
await loadFixtures(dbStates);
for (const index of indices) {
await client.deleteByQuery({
index,
body: {
query: {
match_all: {},
},
},
refresh: 'true',
});
}
});

for (const index of indices) {
Expand Down
24 changes: 24 additions & 0 deletions test/postTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import main from 'scripts/cleanupUrls';
import client from 'util/client';

const checkDocs = async () => {
const {body: {hits: {total, hits}}} = await client.search({
_source: 'false'
})

if (total > 0) {
console.log('\x1b[33m');
console.log('WARNING: test db is not cleaned up properly.');
const docs = hits.map(d => `/${d._index}/${d._type}/${d._id}`)
console.log(JSON.stringify(docs, null, 2));
console.log('\x1b[0m');

for (const d of hits) {
await client.delete({index: d._index, type: d._type, id: d._id})
}
throw new Error()
}
}

checkDocs()

8 changes: 8 additions & 0 deletions test/setup.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import 'dotenv/config';

jest.mock(__dirname + '../../src/util/grpc');

jest.mock(__dirname + '../../src/rollbarInstance', () => ({
__esModule: true,
default: { error: jest.fn() },
}));

jest.setTimeout(process.env.JEST_TIMEOUT || 5000);


expect.extend({
toBeNaN(received) {
const pass = isNaN(received);
Expand Down
13 changes: 13 additions & 0 deletions test/testSequencer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const Sequencer = require('@jest/test-sequencer').default;

class TestSequencer extends Sequencer {
sort(tests) {
// Test structure information
// https://github.com/facebook/jest/blob/6b8b1404a1d9254e7d5d90a8934087a9c9899dab/packages/jest-runner/src/types.ts#L17-L21
const copyTests = Array.from(tests);
return copyTests.sort((testA, testB) => (testA.path > testB.path ? 1 : -1));
}
}


module.exports = TestSequencer;
33 changes: 0 additions & 33 deletions test/util/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,36 +60,3 @@ export async function resetFrom(fixtureMap, key) {
refresh: 'true',
});
}

export async function saveStateForIndices(indices) {
let states = {}
for (const index of indices) {
const { body: { hits: { hits } } } = await client.search({
index,
body: {
query: {
match_all: {},
},
},
size: 10000
})
for (const doc of hits) {
states[`/${doc._index}/${doc._type}/${doc._id}`] = { ...doc._source };
}
}
return states
}

export async function clearIndices(indices) {
for (const index of indices) {
await client.deleteByQuery({
index,
body: {
query: {
match_all: {},
},
},
refresh: 'true'
})
}
}

0 comments on commit e23bd1f

Please sign in to comment.