From 11261a0cb94b4458c19b08a4ee3985084a1abc50 Mon Sep 17 00:00:00 2001 From: Eric Mc Sween Date: Fri, 19 Mar 2021 16:04:30 -0400 Subject: [PATCH 1/2] Add global test setup Configure SanboxedModule and chai in a central place. Configure SandboxedModule globals that are needed in Node 12. --- services/spelling/.mocharc.json | 3 +++ services/spelling/test/setup.js | 21 +++++++++++++++++++ services/spelling/test/unit/js/ASpellTests.js | 11 ++-------- .../test/unit/js/ASpellWorkerTests.js | 8 +------ .../test/unit/js/LearnedWordsManagerTests.js | 13 ++---------- .../test/unit/js/SpellingAPIManagerTests.js | 4 +--- 6 files changed, 30 insertions(+), 30 deletions(-) create mode 100644 services/spelling/.mocharc.json create mode 100644 services/spelling/test/setup.js diff --git a/services/spelling/.mocharc.json b/services/spelling/.mocharc.json new file mode 100644 index 0000000000..dc3280aa96 --- /dev/null +++ b/services/spelling/.mocharc.json @@ -0,0 +1,3 @@ +{ + "require": "test/setup.js" +} diff --git a/services/spelling/test/setup.js b/services/spelling/test/setup.js new file mode 100644 index 0000000000..9ebe599cea --- /dev/null +++ b/services/spelling/test/setup.js @@ -0,0 +1,21 @@ +const chai = require('chai') +const SandboxedModule = require('sandboxed-module') + +// Chai configuration +chai.should() + +// SandboxedModule configuration +SandboxedModule.configure({ + requires: { + 'logger-sharelatex': { + debug() {}, + log() {}, + info() {}, + warn() {}, + err() {}, + error() {}, + fatal() {} + } + }, + globals: { Buffer, JSON, console, process } +}) diff --git a/services/spelling/test/unit/js/ASpellTests.js b/services/spelling/test/unit/js/ASpellTests.js index 22798db6f2..9ea535dd3a 100644 --- a/services/spelling/test/unit/js/ASpellTests.js +++ b/services/spelling/test/unit/js/ASpellTests.js @@ -9,20 +9,13 @@ * DS102: Remove unnecessary code created because of implicit returns * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ -const chai = require('chai') -const should = chai.should() +const { expect, assert } = require('chai') const SandboxedModule = require('sandboxed-module') -const { assert } = require('chai') describe('ASpell', function () { beforeEach(function () { return (this.ASpell = SandboxedModule.require('../../../app/js/ASpell', { requires: { - 'logger-sharelatex': { - log() {}, - info() {}, - err() {} - }, '@overleaf/metrics': { gauge() {}, inc() {} @@ -93,7 +86,7 @@ describe('ASpell', function () { }) return it('should return an error', function () { - return should.exist(this.error) + return expect(this.error).to.exist }) }) diff --git a/services/spelling/test/unit/js/ASpellWorkerTests.js b/services/spelling/test/unit/js/ASpellWorkerTests.js index 6fbd6d805e..aa85be41cc 100644 --- a/services/spelling/test/unit/js/ASpellWorkerTests.js +++ b/services/spelling/test/unit/js/ASpellWorkerTests.js @@ -3,8 +3,7 @@ no-undef */ const sinon = require('sinon') -const chai = require('chai') -const { expect } = chai +const { expect } = require('chai') const SandboxedModule = require('sandboxed-module') const EventEmitter = require('events') @@ -15,11 +14,6 @@ describe('ASpellWorker', function () { '../../../app/js/ASpellWorker', { requires: { - 'logger-sharelatex': { - log() {}, - info() {}, - err() {} - }, '@overleaf/metrics': { gauge() {}, inc() {} diff --git a/services/spelling/test/unit/js/LearnedWordsManagerTests.js b/services/spelling/test/unit/js/LearnedWordsManagerTests.js index 16aa763f2c..99249a2c39 100644 --- a/services/spelling/test/unit/js/LearnedWordsManagerTests.js +++ b/services/spelling/test/unit/js/LearnedWordsManagerTests.js @@ -1,12 +1,11 @@ const sinon = require('sinon') -const chai = require('chai') -const { expect } = chai +const { assert, expect } = require('chai') const SandboxedModule = require('sandboxed-module') const modulePath = require('path').join( __dirname, '../../../app/js/LearnedWordsManager' ) -const { assert } = require('chai') + describe('LearnedWordsManager', function () { beforeEach(function () { this.token = 'a6b3cd919ge' @@ -22,17 +21,9 @@ describe('LearnedWordsManager', function () { del: sinon.stub() } this.LearnedWordsManager = SandboxedModule.require(modulePath, { - globals: { - console: console - }, requires: { './mongodb': { db: this.db }, './MongoCache': this.cache, - 'logger-sharelatex': { - log() {}, - err() {}, - info() {} - }, '@overleaf/metrics': { timeAsyncMethod: sinon.stub(), inc: sinon.stub() diff --git a/services/spelling/test/unit/js/SpellingAPIManagerTests.js b/services/spelling/test/unit/js/SpellingAPIManagerTests.js index 76d592720f..8cf1d0c694 100644 --- a/services/spelling/test/unit/js/SpellingAPIManagerTests.js +++ b/services/spelling/test/unit/js/SpellingAPIManagerTests.js @@ -2,9 +2,7 @@ handle-callback-err */ const sinon = require('sinon') -const chai = require('chai') -const { expect } = chai -chai.should() +const { expect } = require('chai') const SandboxedModule = require('sandboxed-module') const modulePath = require('path').join( __dirname, From 0bc76bfdfde8a6741baf0b8d2a05726053aa68d2 Mon Sep 17 00:00:00 2001 From: Eric Mc Sween Date: Fri, 19 Mar 2021 16:05:19 -0400 Subject: [PATCH 2/2] Upgrade to Node 12 --- services/spelling/.nvmrc | 2 +- services/spelling/Dockerfile | 2 +- services/spelling/Makefile | 6 ++++-- services/spelling/buildscript.txt | 4 ++-- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/services/spelling/.nvmrc b/services/spelling/.nvmrc index 2baa2d433a..e68b860383 100644 --- a/services/spelling/.nvmrc +++ b/services/spelling/.nvmrc @@ -1 +1 @@ -10.23.1 +12.21.0 diff --git a/services/spelling/Dockerfile b/services/spelling/Dockerfile index 1fcc677d92..185686be4d 100644 --- a/services/spelling/Dockerfile +++ b/services/spelling/Dockerfile @@ -2,7 +2,7 @@ # Instead run bin/update_build_scripts from # https://github.com/sharelatex/sharelatex-dev-environment -FROM node:10.23.1 as base +FROM node:12.21.0 as base WORKDIR /app COPY install_deps.sh /app diff --git a/services/spelling/Makefile b/services/spelling/Makefile index e698a356e9..d0a88a8c23 100644 --- a/services/spelling/Makefile +++ b/services/spelling/Makefile @@ -21,8 +21,10 @@ DOCKER_COMPOSE_TEST_UNIT = \ COMPOSE_PROJECT_NAME=test_unit_$(BUILD_DIR_NAME) $(DOCKER_COMPOSE) clean: - docker rmi ci/$(PROJECT_NAME):$(BRANCH_NAME)-$(BUILD_NUMBER) - docker rmi gcr.io/overleaf-ops/$(PROJECT_NAME):$(BRANCH_NAME)-$(BUILD_NUMBER) + -docker rmi ci/$(PROJECT_NAME):$(BRANCH_NAME)-$(BUILD_NUMBER) + -docker rmi gcr.io/overleaf-ops/$(PROJECT_NAME):$(BRANCH_NAME)-$(BUILD_NUMBER) + -$(DOCKER_COMPOSE_TEST_UNIT) down --rmi local + -$(DOCKER_COMPOSE_TEST_ACCEPTANCE) down --rmi local format: $(DOCKER_COMPOSE) run --rm test_unit npm run --silent format diff --git a/services/spelling/buildscript.txt b/services/spelling/buildscript.txt index 4f4c994f01..f31500afcc 100644 --- a/services/spelling/buildscript.txt +++ b/services/spelling/buildscript.txt @@ -4,6 +4,6 @@ spelling --docker-repos=gcr.io/overleaf-ops --env-add= --env-pass-through= ---node-version=10.23.1 +--node-version=12.21.0 --public-repo=False ---script-version=3.4.0 +--script-version=3.7.0