From c62f2b7d0bcd2b75142960ebfa02a1fe33746724 Mon Sep 17 00:00:00 2001 From: Michael Bromley Date: Wed, 29 Jan 2020 15:59:23 +0100 Subject: [PATCH] chore: Test multiple DBs in CI e2e tests Closes #207 * chore: Attempt multi DB CI setup * chore: Add postgres test * chore: Add postgres test fix * chore: Add elasticsearch test * chore: Fix yaml formatting * chore: Fix yaml formatting again * chore: Wip update build and test workflow * chore(core): Configure GH workflow for multiple dbs * chore(core): Re-enable build workflow * chore(core): Fix yaml * chore(core): Fix yaml again * chore(core): Fix yaml again * chore(core): Fixage * chore(core): Fix mysql host * test(core): Fix custom fields e2e test for mariaDB 10.3 * chore(test): Remove direct imports from TS source files May improve e2e test times? * test(elasticsearch-plugin): Use http protocol in CI --- .github/workflows/build_and_test.yml | 96 ++++++++++++++----- e2e-common/custom-reporter.js | 8 ++ e2e-common/e2e-initial-data.ts | 3 +- e2e-common/jest-config.js | 25 ++--- e2e-common/test-config.ts | 6 +- packages/common/package.json | 3 +- packages/core/e2e/custom-fields.e2e-spec.ts | 4 +- .../core/e2e/entity-uuid-strategy.e2e-spec.ts | 15 ++- packages/core/e2e/fixtures/test-plugins.ts | 11 +-- packages/core/package.json | 3 +- .../e2e/elasticsearch-plugin.e2e-spec.ts | 6 +- packages/testing/package.json | 3 +- 12 files changed, 117 insertions(+), 66 deletions(-) create mode 100644 e2e-common/custom-reporter.js diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml index 23b9b0cc3a..33c57b8172 100644 --- a/.github/workflows/build_and_test.yml +++ b/.github/workflows/build_and_test.yml @@ -8,41 +8,85 @@ on: branches: - master +env: + CI: true + node: 12.x jobs: build: - + name: build runs-on: ubuntu-latest - - strategy: - matrix: - node-version: [10.x, 12.x] - - env: - CI: true - steps: - uses: actions/checkout@v1 - - name: Use Node.js ${{ matrix.node-version }} + - name: Use Node.js ${{ env.node }} uses: actions/setup-node@v1 with: - node-version: ${{ matrix.node-version }} - # This is required for the elasticsearch-plugin e2e tests. It would be better to use the - # "services" feature, but I cannot figure out how to connect to it from the test server. - - name: Install elasticsearch - run: | - curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.4.2-linux-x86_64.tar.gz - tar -xvf elasticsearch-7.4.2-linux-x86_64.tar.gz - cd elasticsearch-7.4.2/bin - ./elasticsearch & + node-version: ${{ env.node }} - name: Install & build run: | yarn install yarn bootstrap yarn build - - name: Unit tests - run: yarn test - - name: e2e tests - env: - ELASTICSEARCH_PORT: 9200 - ELASTICSEARCH_HOST: http://localhost - run: yarn e2e + unit-tests: + name: unit tests + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + - name: Use Node.js ${{ env.node }} + uses: actions/setup-node@v1 + with: + node-version: ${{ env.node }} + - name: Install & build + run: | + yarn install + yarn bootstrap + yarn lerna run ci + - name: Unit tests + run: yarn test + e2e-tests: + name: e2e tests + runs-on: ubuntu-latest + services: + mysql: + image: bitnami/mariadb:10.3 + env: + ALLOW_EMPTY_PASSWORD: yes + ports: + - 3306 + options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3 + postgres: + image: postgres:12 + env: + POSTGRES_PASSWORD: Be70 + ports: + - 5432 + options: --health-cmd=pg_isready --health-interval=10s --health-timeout=5s --health-retries=3 + elastic: + image: docker.elastic.co/elasticsearch/elasticsearch:7.1.1 + env: + discovery.type: single-node + bootstrap.memory_lock: true + ES_JAVA_OPTS: -Xms512m -Xmx512m + ports: + - 9200 + options: --health-cmd="curl --silent --fail localhost:9200/_cluster/health" --health-interval=10s --health-timeout=5s --health-retries=3 + strategy: + matrix: + db: [sqljs, mysql, postgres] + steps: + - uses: actions/checkout@v1 + - name: Use Node.js ${{ env.node }} + uses: actions/setup-node@v1 + with: + node-version: ${{ env.node }} + - name: Install & build + run: | + yarn install + yarn bootstrap + yarn lerna run ci + - name: e2e tests + env: + E2E_MYSQL_PORT: ${{ job.services.mysql.ports['3306'] }} + E2E_POSTGRES_PORT: ${{ job.services.postgres.ports['5432'] }} + E2E_ELASTIC_PORT: ${{ job.services.elastic.ports['9200'] }} + DB: ${{ matrix.db }} + run: yarn e2e diff --git a/e2e-common/custom-reporter.js b/e2e-common/custom-reporter.js new file mode 100644 index 0000000000..38202f17f4 --- /dev/null +++ b/e2e-common/custom-reporter.js @@ -0,0 +1,8 @@ +class MyCustomReporter { + onRunComplete(contexts, results) { + const dbType = process.env.DB || 'sqljs'; + console.log(`Database engine: ${dbType}`); + } +} + +module.exports = MyCustomReporter; diff --git a/e2e-common/e2e-initial-data.ts b/e2e-common/e2e-initial-data.ts index f48e122c42..ac6c1ddb49 100644 --- a/e2e-common/e2e-initial-data.ts +++ b/e2e-common/e2e-initial-data.ts @@ -1,6 +1,5 @@ import { LanguageCode } from '@vendure/common/lib/generated-types'; - -import { InitialData } from '../packages/core/src/data-import/index'; +import { InitialData } from '@vendure/core/dist/data-import/index'; export const initialData: InitialData = { defaultLanguage: LanguageCode.en, diff --git a/e2e-common/jest-config.js b/e2e-common/jest-config.js index 1232978f25..ecb3e1711c 100644 --- a/e2e-common/jest-config.js +++ b/e2e-common/jest-config.js @@ -4,17 +4,18 @@ const { getPackageDir } = require('./get-package-dir'); const packageDirname = getPackageDir(); module.exports = { - 'moduleFileExtensions': ['js', 'json', 'ts'], - 'rootDir': packageDirname, - 'testRegex': '.e2e-spec.ts$', - 'transform': { - '^.+\\.(t|j)s$': 'ts-jest', - }, - 'testEnvironment': 'node', - 'globals': { - 'ts-jest': { - 'tsConfig': '/config/tsconfig.e2e.json', - 'diagnostics': false, + moduleFileExtensions: ['js', 'json', 'ts'], + rootDir: packageDirname, + testRegex: '.e2e-spec.ts$', + transform: { + '^.+\\.(t|j)s$': 'ts-jest', + }, + testEnvironment: 'node', + reporters: ['default', path.join(__dirname, 'custom-reporter.js')], + globals: { + 'ts-jest': { + tsConfig: '/config/tsconfig.e2e.json', + diagnostics: false, + }, }, - }, }; diff --git a/e2e-common/test-config.ts b/e2e-common/test-config.ts index 5b2909321a..5151ae1011 100644 --- a/e2e-common/test-config.ts +++ b/e2e-common/test-config.ts @@ -43,7 +43,7 @@ function getDbConfig(): ConnectionOptions { synchronize: true, type: 'postgres', host: '127.0.0.1', - port: 5432, + port: process.env.CI ? +(process.env.E2E_POSTGRES_PORT || 5432) : 5432, username: 'postgres', password: 'Be70', }; @@ -51,8 +51,8 @@ function getDbConfig(): ConnectionOptions { return { synchronize: true, type: 'mysql', - host: '192.168.99.100', - port: 3306, + host: process.env.CI ? '127.0.0.1' : '192.168.99.100', + port: process.env.CI ? +(process.env.E2E_MYSQL_PORT || 3306) : 3306, username: 'root', password: '', }; diff --git a/packages/common/package.json b/packages/common/package.json index d08a0aac0c..b4612d6763 100644 --- a/packages/common/package.json +++ b/packages/common/package.json @@ -7,7 +7,8 @@ "watch": "tsc -p ./tsconfig.build.json -w", "build": "rimraf dist && tsc -p ./tsconfig.build.json", "lint": "tslint --fix --project ./", - "test": "jest --config ./jest.config.js" + "test": "jest --config ./jest.config.js", + "ci": "yarn build" }, "publishConfig": { "access": "public" diff --git a/packages/core/e2e/custom-fields.e2e-spec.ts b/packages/core/e2e/custom-fields.e2e-spec.ts index 672112b8a2..f018d6d526 100644 --- a/packages/core/e2e/custom-fields.e2e-spec.ts +++ b/packages/core/e2e/custom-fields.e2e-spec.ts @@ -92,7 +92,7 @@ const customConfig = mergeConfig(testConfig, { { name: 'longString', type: 'string', - length: 60000, + length: 10000, }, { name: 'readonlyString', @@ -317,7 +317,7 @@ describe('Custom fields', () => { ); it('string length allows long strings', async () => { - const longString = Array.from({ length: 4000 }, v => 'hello there!').join(' '); + const longString = Array.from({ length: 500 }, v => 'hello there!').join(' '); const result = await adminClient.query( gql` mutation($stringValue: String!) { diff --git a/packages/core/e2e/entity-uuid-strategy.e2e-spec.ts b/packages/core/e2e/entity-uuid-strategy.e2e-spec.ts index 53ac3db834..f0bfe1fa72 100644 --- a/packages/core/e2e/entity-uuid-strategy.e2e-spec.ts +++ b/packages/core/e2e/entity-uuid-strategy.e2e-spec.ts @@ -1,22 +1,21 @@ /* tslint:disable:no-non-null-assertion */ import { UuidIdStrategy } from '@vendure/core'; +// This import is here to simulate the behaviour of +// the package end-user importing symbols from the +// @vendure/core barrel file. Doing so will then cause the +// recursive evaluation of all imported files. This tests +// the resilience of the id strategy implementation to the +// order of file evaluation. +import '@vendure/core/dist/index'; import { createTestEnvironment } from '@vendure/testing'; import path from 'path'; import { initialData } from '../../../e2e-common/e2e-initial-data'; import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config'; -import '../src/index'; import { GetProductList } from './graphql/generated-e2e-admin-types'; import { GET_PRODUCT_LIST } from './graphql/shared-definitions'; -// This import is here to simulate the behaviour of -// the package end-user importing symbols from the -// @vendure/core barrel file. Doing so will then cause the -// recusrsive evaluation of all imported files. This tests -// the resilience of the id strategy implementation to the -// order of file evaluation. - describe('UuidIdStrategy', () => { const { server, adminClient } = createTestEnvironment({ ...testConfig, diff --git a/packages/core/e2e/fixtures/test-plugins.ts b/packages/core/e2e/fixtures/test-plugins.ts index 3f35bce1a9..9b7b33f8f6 100644 --- a/packages/core/e2e/fixtures/test-plugins.ts +++ b/packages/core/e2e/fixtures/test-plugins.ts @@ -1,18 +1,17 @@ import { Injectable, OnApplicationBootstrap, OnModuleDestroy, OnModuleInit } from '@nestjs/common'; import { Query, Resolver } from '@nestjs/graphql'; import { LanguageCode } from '@vendure/common/lib/generated-types'; -import gql from 'graphql-tag'; - -import { VendureConfig } from '../../src/config'; -import { ConfigModule } from '../../src/config/config.module'; -import { ConfigService } from '../../src/config/config.service'; import { + ConfigService, OnVendureBootstrap, OnVendureClose, OnVendureWorkerBootstrap, OnVendureWorkerClose, + VendureConfig, VendurePlugin, -} from '../../src/plugin/vendure-plugin'; +} from '@vendure/core'; +import { ConfigModule } from '@vendure/core/dist/config/config.module'; +import gql from 'graphql-tag'; export class TestPluginWithAllLifecycleHooks implements OnVendureBootstrap, OnVendureWorkerBootstrap, OnVendureClose, OnVendureWorkerClose { diff --git a/packages/core/package.json b/packages/core/package.json index 004a29a5da..c3b90ed548 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -22,7 +22,8 @@ "watch": "concurrently yarn:tsc:watch yarn:gulp:watch", "lint": "tslint --fix --project ./", "test": "jest --config ./jest.config.js", - "e2e": "jest --config ../../e2e-common/jest-config.js --runInBand --package=core" + "e2e": "jest --config ../../e2e-common/jest-config.js --runInBand --package=core", + "ci": "yarn build" }, "publishConfig": { "access": "public" diff --git a/packages/elasticsearch-plugin/e2e/elasticsearch-plugin.e2e-spec.ts b/packages/elasticsearch-plugin/e2e/elasticsearch-plugin.e2e-spec.ts index e7679d7ca3..b1152df9da 100644 --- a/packages/elasticsearch-plugin/e2e/elasticsearch-plugin.e2e-spec.ts +++ b/packages/elasticsearch-plugin/e2e/elasticsearch-plugin.e2e-spec.ts @@ -53,10 +53,8 @@ describe('Elasticsearch plugin', () => { plugins: [ ElasticsearchPlugin.init({ indexPrefix: 'e2e-tests', - port: process.env.CI ? +(process.env.ELASTICSEARCH_PORT || 9200) : 9200, - host: process.env.CI - ? process.env.ELASTICSEARCH_HOST || 'elasticsearch' - : 'http://192.168.99.100', + port: process.env.CI ? +(process.env.E2E_ELASTIC_PORT || 9200) : 9200, + host: process.env.CI ? 'http://127.0.0.1' : 'http://192.168.99.100', }), ], }), diff --git a/packages/testing/package.json b/packages/testing/package.json index 86293937dc..f968444ea1 100644 --- a/packages/testing/package.json +++ b/packages/testing/package.json @@ -25,7 +25,8 @@ "scripts": { "build": "tsc -p ./tsconfig.build.json", "watch": "tsc -p ./tsconfig.build.json -w", - "lint": "tslint --fix --project ./" + "lint": "tslint --fix --project ./", + "ci": "yarn build" }, "bugs": { "url": "https://github.com/vendure-ecommerce/vendure/issues"