Skip to content

Commit

Permalink
Add Jest; DoctrineArray tests (#9)
Browse files Browse the repository at this point in the history
* Add Jest; DoctrineArray tests

* Run tests in CI
  • Loading branch information
karashiiro authored Jul 5, 2022
1 parent 68bdf4c commit 3aab805
Show file tree
Hide file tree
Showing 8 changed files with 2,203 additions and 78 deletions.
52 changes: 52 additions & 0 deletions db/DoctrineArray.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { DoctrineArray } from './DoctrineArray';

test('DoctrineArray can serialize an empty array', () => {
const doctrine = new DoctrineArray();
const serialized = doctrine.serialize();
expect(serialized).toEqual('a:0:{}');
});

test('DoctrineArray can serialize an array with one number', () => {
const doctrine = new DoctrineArray();
doctrine.push(33027);
const serialized = doctrine.serialize();
expect(serialized).toEqual('a:1:{i:0;i:33027;}');
});

test('DoctrineArray can serialize an array with two numbers', () => {
const doctrine = new DoctrineArray();
doctrine.push(...[33027, 5333]);
const serialized = doctrine.serialize();
expect(serialized).toEqual('a:2:{i:0;i:33027;i:1;i:5333;}');
});

test('DoctrineArray can deserialize an empty array', () => {
const doctrine = DoctrineArray.deserialize('a:0:{}');
expect(doctrine).toHaveLength(0);
});

test('DoctrineArray can deserialize an array with one number', () => {
const doctrine = DoctrineArray.deserialize('a:1:{i:0;i:33027;}');
expect(doctrine).toHaveLength(1);
expect(doctrine[0]).toStrictEqual(33027);
});

test('DoctrineArray can deserialize an array with two numbers', () => {
const doctrine = DoctrineArray.deserialize('a:2:{i:0;i:33027;i:1;i:5333;}');
expect(doctrine).toHaveLength(2);
expect(doctrine[0]).toStrictEqual(33027);
expect(doctrine[1]).toStrictEqual(5333);
});

test('DoctrineArray can serialize and deserialize an array with many numbers', () => {
const data = new Array(1000).fill(0).map(() => Math.floor(Math.random() * 1000));
const doctrine = new DoctrineArray();
doctrine.push(...data);
const serialized = doctrine.serialize();
const result = DoctrineArray.deserialize(serialized);

expect(result).toHaveLength(data.length);
for (let i = 0; i < data.length; i++) {
expect(result[i]).toStrictEqual(data[i]);
}
});
2 changes: 1 addition & 1 deletion db/DoctrineArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ enum DoctrineArrayParseState {
}

class ArrayWithEnd<T> extends Array<T> {
end() {
public end() {
return this[this.length - 1];
}
}
Expand Down
2 changes: 1 addition & 1 deletion docker/development/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ COPY . .
ENV NEXT_TELEMETRY_DISABLED 1

COPY .env.development .env.production
RUN yarn build
RUN yarn test && yarn build

# Production image, copy all the files and run next
FROM node:16-alpine AS runner
Expand Down
2 changes: 1 addition & 1 deletion docker/production/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ COPY . .

ENV NEXT_TELEMETRY_DISABLED 1

RUN yarn build
RUN yarn test && yarn build

# Production image, copy all the files and run next
FROM node:16-alpine AS runner
Expand Down
2 changes: 1 addition & 1 deletion docker/staging/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ COPY . .
ENV NEXT_TELEMETRY_DISABLED 1

COPY .env.staging .env.production
RUN yarn build
RUN yarn test && yarn build

# Production image, copy all the files and run next
FROM node:16-alpine AS runner
Expand Down
39 changes: 39 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module.exports = {
collectCoverage: true,
// on node 14.x coverage provider v8 offers good speed and more or less good report
coverageProvider: 'v8',
collectCoverageFrom: [
'**/*.{js,jsx,ts,tsx}',
'!**/*.d.ts',
'!**/node_modules/**',
'!<rootDir>/out/**',
'!<rootDir>/.next/**',
'!<rootDir>/*.config.js',
'!<rootDir>/coverage/**',
],
moduleNameMapper: {
// Handle CSS imports (with CSS modules)
// https://jestjs.io/docs/webpack#mocking-css-modules
'^.+\\.module\\.(css|sass|scss)$': 'identity-obj-proxy',

// Handle CSS imports (without CSS modules)
'^.+\\.(css|sass|scss)$': '<rootDir>/__mocks__/styleMock.js',

// Handle image imports
// https://jestjs.io/docs/webpack#handling-static-assets
'^.+\\.(png|jpg|jpeg|gif|webp|avif|ico|bmp|svg)$/i': `<rootDir>/__mocks__/fileMock.js`,

// Handle module aliases
'^@/components/(.*)$': '<rootDir>/components/$1',
},
// Add more setup options before each test is run
// setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
testPathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/.next/'],
testEnvironment: 'jsdom',
transform: {
// Use babel-jest to transpile tests with the next/babel preset
// https://jestjs.io/docs/configuration#transform-objectstring-pathtotransformer--pathtotransformer-object
'^.+\\.(js|jsx|ts|tsx)$': ['babel-jest', { presets: ['next/babel'] }],
},
transformIgnorePatterns: ['/node_modules/', '^.+\\.module\\.(css|sass|scss)$'],
};
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "mogboard-next",
"version": "0.1.0",
"version": "1.0.2",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint",
"test": "jest",
"analyze": "cross-env ANALYZE=true next build",
"prettier:check": "prettier --check \"**/*.{ts,tsx,css,scss}\"",
"prettier:write": "prettier --write \"**/*.{ts,tsx,css,scss}\"",
Expand Down Expand Up @@ -37,15 +38,21 @@
"devDependencies": {
"@babel/core": "^7.18.5",
"@lingui/cli": "^3.14.0",
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.3.0",
"@types/node": "18.0.0",
"@types/react": "18.0.14",
"@types/react-dom": "18.0.5",
"@types/sprintf-js": "^1.1.2",
"@types/uuid": "^8.3.4",
"babel-jest": "^28.1.2",
"babel-plugin-macros": "^3.1.0",
"cross-env": "^7.0.3",
"eslint": "8.18.0",
"eslint-config-next": "^12.2.0",
"identity-obj-proxy": "^3.0.0",
"jest": "^28.1.2",
"jest-environment-jsdom": "^28.1.2",
"prettier": "^2.7.1",
"sass": "^1.52.3",
"typescript": "4.7.4"
Expand Down
Loading

0 comments on commit 3aab805

Please sign in to comment.