From 8554df5c612ba5035c4f9b7061a4b45ca59b3dbb Mon Sep 17 00:00:00 2001
From: Jamie Magee <JamieMagee@users.noreply.github.com>
Date: Wed, 25 Sep 2019 11:40:58 +0200
Subject: [PATCH] test: convert top level tests to typescript (#4526)

Also convert usage of chai to jest
---
 package.json                                  |  8 +---
 test/chai.js                                  |  1 -
 test/config/cli.spec.ts                       | 38 +++++++++----------
 test/config/env.spec.ts                       |  6 +--
 test/{globals.js => globals.ts}               | 12 +++---
 test/manager/npm/update.spec.ts               |  8 ++--
 test/newline-snapshot-serializer.js           | 15 --------
 test/newline-snapshot-serializer.ts           | 12 ++++++
 test/{proxy.spec.js => proxy.spec.ts}         |  0
 test/{renovate.spec.js => renovate.spec.ts}   |  5 ++-
 ...tic-files.spec.js => static-files.spec.ts} |  9 +++--
 ...site-docs.spec.js => website-docs.spec.ts} |  6 ++-
 yarn.lock                                     |  2 +-
 13 files changed, 59 insertions(+), 63 deletions(-)
 delete mode 100644 test/chai.js
 rename test/{globals.js => globals.ts} (53%)
 delete mode 100644 test/newline-snapshot-serializer.js
 create mode 100644 test/newline-snapshot-serializer.ts
 rename test/{proxy.spec.js => proxy.spec.ts} (100%)
 rename test/{renovate.spec.js => renovate.spec.ts} (58%)
 rename test/{static-files.spec.js => static-files.spec.ts} (76%)
 rename test/{website-docs.spec.js => website-docs.spec.ts} (94%)

diff --git a/package.json b/package.json
index bbb8c01d8d..ed4d602cfc 100644
--- a/package.json
+++ b/package.json
@@ -197,7 +197,6 @@
     "aws-sdk-mock": "4.5.0",
     "babel-jest": "24.9.0",
     "babel-plugin-dynamic-import-node": "2.3.0",
-    "chai": "4.2.0",
     "copyfiles": "2.1.1",
     "cross-env": "5.2.1",
     "eslint": "6.4.0",
@@ -256,13 +255,10 @@
       }
     },
     "setupFiles": [
-      "./test/globals.js"
-    ],
-    "setupFilesAfterEnv": [
-      "./test/chai.js"
+      "./test/globals.ts"
     ],
     "snapshotSerializers": [
-      "./test/newline-snapshot-serializer.js"
+      "./test/newline-snapshot-serializer.ts"
     ],
     "transform": {
       "^.+\\.(j|t)s$": "babel-jest"
diff --git a/test/chai.js b/test/chai.js
deleted file mode 100644
index 41e09fc538..0000000000
--- a/test/chai.js
+++ /dev/null
@@ -1 +0,0 @@
-require('chai').should();
diff --git a/test/config/cli.spec.ts b/test/config/cli.spec.ts
index e18b434c26..24b4423029 100644
--- a/test/config/cli.spec.ts
+++ b/test/config/cli.spec.ts
@@ -12,29 +12,29 @@ describe('config/cli', () => {
       const option: Partial<RenovateOptions> = {
         name: 'oneTwoThree',
       };
-      cli.getCliName(option).should.eql('--one-two-three');
+      expect(cli.getCliName(option)).toEqual('--one-two-three');
     });
     it('generates returns empty if CLI false', () => {
       const option: Partial<RenovateOptions> = {
         name: 'oneTwoThree',
         cli: false,
       };
-      cli.getCliName(option).should.eql('');
+      expect(cli.getCliName(option)).toEqual('');
     });
   });
   describe('.getConfig(argv)', () => {
     it('returns empty argv', () => {
-      cli.getConfig(argv).should.eql({});
+      expect(cli.getConfig(argv)).toEqual({});
     });
     it('supports boolean no value', () => {
       argv.push('--recreate-closed');
-      cli.getConfig(argv).should.eql({ recreateClosed: true });
+      expect(cli.getConfig(argv)).toEqual({ recreateClosed: true });
       argv = argv.slice(0, -1);
     });
     it('supports boolean space true', () => {
       argv.push('--recreate-closed');
       argv.push('true');
-      cli.getConfig(argv).should.eql({ recreateClosed: true });
+      expect(cli.getConfig(argv)).toEqual({ recreateClosed: true });
     });
     it('throws exception for invalid boolean value', () => {
       argv.push('--recreate-closed');
@@ -48,38 +48,38 @@ describe('config/cli', () => {
     it('supports boolean space false', () => {
       argv.push('--recreate-closed');
       argv.push('false');
-      cli.getConfig(argv).should.eql({ recreateClosed: false });
+      expect(cli.getConfig(argv)).toEqual({ recreateClosed: false });
     });
     it('supports boolean equals true', () => {
       argv.push('--recreate-closed=true');
-      cli.getConfig(argv).should.eql({ recreateClosed: true });
+      expect(cli.getConfig(argv)).toEqual({ recreateClosed: true });
     });
     it('supports boolean equals false', () => {
       argv.push('--recreate-closed=false');
-      cli.getConfig(argv).should.eql({ recreateClosed: false });
+      expect(cli.getConfig(argv)).toEqual({ recreateClosed: false });
     });
     it('supports list single', () => {
       argv.push('--labels=a');
-      cli.getConfig(argv).should.eql({ labels: ['a'] });
+      expect(cli.getConfig(argv)).toEqual({ labels: ['a'] });
     });
     it('supports list multiple', () => {
       argv.push('--labels=a,b,c');
-      cli.getConfig(argv).should.eql({ labels: ['a', 'b', 'c'] });
+      expect(cli.getConfig(argv)).toEqual({ labels: ['a', 'b', 'c'] });
     });
     it('supports string', () => {
       argv.push('--token=a');
-      cli.getConfig(argv).should.eql({ token: 'a' });
+      expect(cli.getConfig(argv)).toEqual({ token: 'a' });
     });
     it('supports repositories', () => {
       argv.push('foo');
       argv.push('bar');
-      cli.getConfig(argv).should.eql({ repositories: ['foo', 'bar'] });
+      expect(cli.getConfig(argv)).toEqual({ repositories: ['foo', 'bar'] });
     });
     it('parses json lists correctly', () => {
       argv.push(
         `--host-rules=[{"domainName":"docker.io","hostType":"docker","username":"user","password":"password"}]`
       );
-      cli.getConfig(argv).should.deep.equal({
+      expect(cli.getConfig(argv)).toEqual({
         hostRules: [
           {
             domainName: 'docker.io',
@@ -92,37 +92,37 @@ describe('config/cli', () => {
     });
     it('parses [] correctly as empty list of hostRules', () => {
       argv.push(`--host-rules=[]`);
-      cli.getConfig(argv).should.eql({
+      expect(cli.getConfig(argv)).toEqual({
         hostRules: [],
       });
     });
     it('parses an empty string correctly as empty list of hostRules', () => {
       argv.push(`--host-rules=`);
-      cli.getConfig(argv).should.eql({
+      expect(cli.getConfig(argv)).toEqual({
         hostRules: [],
       });
     });
     it('migrates --endpoints', () => {
       argv.push(`--endpoints=`);
-      cli.getConfig(argv).should.eql({
+      expect(cli.getConfig(argv)).toEqual({
         hostRules: [],
       });
     });
     it('parses json object correctly when empty', () => {
       argv.push(`--onboarding-config=`);
-      cli.getConfig(argv).should.deep.equal({
+      expect(cli.getConfig(argv)).toEqual({
         onboardingConfig: {},
       });
     });
     it('parses json {} object correctly', () => {
       argv.push(`--onboarding-config={}`);
-      cli.getConfig(argv).should.deep.equal({
+      expect(cli.getConfig(argv)).toEqual({
         onboardingConfig: {},
       });
     });
     it('parses json object correctly', () => {
       argv.push(`--onboarding-config={"extends": ["config:base"]}`);
-      cli.getConfig(argv).should.deep.equal({
+      expect(cli.getConfig(argv)).toEqual({
         onboardingConfig: {
           extends: ['config:base'],
         },
diff --git a/test/config/env.spec.ts b/test/config/env.spec.ts
index e7886e7916..401b387d56 100644
--- a/test/config/env.spec.ts
+++ b/test/config/env.spec.ts
@@ -112,20 +112,20 @@ describe('config/env', () => {
         name: 'foo',
         env: false,
       };
-      env.getEnvName(option).should.eql('');
+      expect(env.getEnvName(option)).toEqual('');
     });
     it('returns existing env', () => {
       const option: Partial<RenovateOptions> = {
         name: 'foo',
         env: 'FOO',
       };
-      env.getEnvName(option).should.eql('FOO');
+      expect(env.getEnvName(option)).toEqual('FOO');
     });
     it('generates RENOVATE_ env', () => {
       const option: Partial<RenovateOptions> = {
         name: 'oneTwoThree',
       };
-      env.getEnvName(option).should.eql('RENOVATE_ONE_TWO_THREE');
+      expect(env.getEnvName(option)).toEqual('RENOVATE_ONE_TWO_THREE');
     });
   });
 });
diff --git a/test/globals.js b/test/globals.ts
similarity index 53%
rename from test/globals.js
rename to test/globals.ts
index e485849aec..25fda8e2d7 100644
--- a/test/globals.js
+++ b/test/globals.ts
@@ -1,6 +1,6 @@
-const upath = require('upath');
-const os = require('os');
-const cache = require('../lib/workers/global/cache');
+import { join } from 'upath';
+import { tmpdir } from 'os';
+import { init } from '../lib/workers/global/cache';
 
 jest.mock('../lib/platform', () => ({
   platform: jest.genMockFromModule('../lib/platform/github'),
@@ -10,7 +10,7 @@ jest.mock('../lib/logger');
 
 global.repoCache = {};
 
-const tmpDir = process.env.RENOVATE_TMPDIR || process.env.TMPDIR || os.tmpdir();
-const cacheDir = upath.join(tmpDir, './renovate/cache/renovate');
+const tmpDir = process.env.RENOVATE_TMPDIR || process.env.TMPDIR || tmpdir();
+const cacheDir = join(tmpDir, './renovate/cache/renovate');
 
-cache.init(cacheDir);
+init(cacheDir);
diff --git a/test/manager/npm/update.spec.ts b/test/manager/npm/update.spec.ts
index da7cebc1f7..6eaed7f3c5 100644
--- a/test/manager/npm/update.spec.ts
+++ b/test/manager/npm/update.spec.ts
@@ -23,7 +23,7 @@ describe('workers/branch/package-json', () => {
       };
       const outputContent = readFixture('outputs/011.json');
       const testContent = npmUpdater.updateDependency(input01Content, upgrade);
-      testContent.should.equal(outputContent);
+      expect(testContent).toEqual(outputContent);
     });
     it('replaces a github dependency value', () => {
       const upgrade = {
@@ -124,7 +124,7 @@ describe('workers/branch/package-json', () => {
       };
       const outputContent = readFixture('outputs/012.json');
       const testContent = npmUpdater.updateDependency(input01Content, upgrade);
-      testContent.should.equal(outputContent);
+      expect(testContent).toEqual(outputContent);
     });
     it('replaces only the second instance of a value', () => {
       const upgrade = {
@@ -134,7 +134,7 @@ describe('workers/branch/package-json', () => {
       };
       const outputContent = readFixture('outputs/013.json');
       const testContent = npmUpdater.updateDependency(input01Content, upgrade);
-      testContent.should.equal(outputContent);
+      expect(testContent).toEqual(outputContent);
     });
     it('handles the case where the desired version is already supported', () => {
       const upgrade = {
@@ -143,7 +143,7 @@ describe('workers/branch/package-json', () => {
         newValue: '1.5.8',
       };
       const testContent = npmUpdater.updateDependency(input01Content, upgrade);
-      testContent.should.equal(input01Content);
+      expect(testContent).toEqual(input01Content);
     });
     it('returns null if throws error', () => {
       const upgrade = {
diff --git a/test/newline-snapshot-serializer.js b/test/newline-snapshot-serializer.js
deleted file mode 100644
index 952af90d3e..0000000000
--- a/test/newline-snapshot-serializer.js
+++ /dev/null
@@ -1,15 +0,0 @@
-let prev;
-
-module.exports = {
-  print(val) {
-    return JSON.stringify(val);
-  },
-
-  test(val) {
-    if (['prBody', 'prTitle'].some(str => str === prev)) {
-      return typeof val === 'string' && val.indexOf('\n') > -1;
-    }
-    prev = val;
-    return false;
-  },
-};
diff --git a/test/newline-snapshot-serializer.ts b/test/newline-snapshot-serializer.ts
new file mode 100644
index 0000000000..4a1c219f18
--- /dev/null
+++ b/test/newline-snapshot-serializer.ts
@@ -0,0 +1,12 @@
+let prev: string;
+
+export function print(val: any) {
+  return JSON.stringify(val);
+}
+export function test(val: any) {
+  if (['prBody', 'prTitle'].some(str => str === prev)) {
+    return typeof val === 'string' && val.includes('\n');
+  }
+  prev = val;
+  return false;
+}
diff --git a/test/proxy.spec.js b/test/proxy.spec.ts
similarity index 100%
rename from test/proxy.spec.js
rename to test/proxy.spec.ts
diff --git a/test/renovate.spec.js b/test/renovate.spec.ts
similarity index 58%
rename from test/renovate.spec.js
rename to test/renovate.spec.ts
index c6e9afae4a..31379a8cf7 100644
--- a/test/renovate.spec.js
+++ b/test/renovate.spec.ts
@@ -1,7 +1,8 @@
-const renovateWorker = require('../lib/workers/global');
+import * as renovateWorker from '../lib/workers/global';
+
 require('../lib/.eslintrc');
 
-renovateWorker.start = jest.fn();
+Object.defineProperty(renovateWorker, 'start', { value: jest.fn() });
 
 describe('renovate', () => {
   it('starts', () => {
diff --git a/test/static-files.spec.js b/test/static-files.spec.ts
similarity index 76%
rename from test/static-files.spec.js
rename to test/static-files.spec.ts
index b4c4568bec..4237df1a59 100644
--- a/test/static-files.spec.js
+++ b/test/static-files.spec.ts
@@ -1,17 +1,18 @@
-const util = require('util');
+import util from 'util';
+
 const glob = util.promisify(require('glob'));
 
 const ignoredExtensions = ['js', 'ts', 'md', 'pyc', 'DS_Store', 'map'];
 
-function filterFiles(files) {
+function filterFiles(files: string[]) {
   return files.filter(file =>
     ignoredExtensions.every(extension => !file.endsWith(`.${extension}`))
   );
 }
 
-async function getFiles(dir) {
+async function getFiles(dir: string) {
   return filterFiles(await glob(`${dir}/**/*`, { dot: true, nodir: true })).map(
-    file => file.replace(`${dir}/`, '')
+    (file: string) => file.replace(`${dir}/`, '')
   );
 }
 
diff --git a/test/website-docs.spec.js b/test/website-docs.spec.ts
similarity index 94%
rename from test/website-docs.spec.js
rename to test/website-docs.spec.ts
index e5c0289fd0..dc27a0268c 100644
--- a/test/website-docs.spec.js
+++ b/test/website-docs.spec.ts
@@ -1,5 +1,7 @@
-const fs = require('fs');
-const options = require('../lib/config/definitions').getOptions();
+import fs from 'fs';
+import { RenovateOptions } from '../lib/config/definitions';
+
+const options: RenovateOptions[] = require('../lib/config/definitions').getOptions();
 
 describe('docs', () => {
   const doc = fs.readFileSync('docs/usage/configuration-options.md', 'utf8');
diff --git a/yarn.lock b/yarn.lock
index 9378e44ff9..19bda84b33 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -2149,7 +2149,7 @@ ccount@^1.0.0:
   resolved "https://registry.yarnpkg.com/ccount/-/ccount-1.0.4.tgz#9cf2de494ca84060a2a8d2854edd6dfb0445f386"
   integrity sha512-fpZ81yYfzentuieinmGnphk0pLkOTMm6MZdVqwd77ROvhko6iujLNGrHH5E7utq3ygWklwfmwuG+A7P+NpqT6w==
 
-chai@4.2.0, chai@^4.1.2:
+chai@^4.1.2:
   version "4.2.0"
   resolved "https://registry.yarnpkg.com/chai/-/chai-4.2.0.tgz#760aa72cf20e3795e84b12877ce0e83737aa29e5"
   integrity sha512-XQU3bhBukrOsQCuwZndwGcCVQHyZi53fQ6Ys1Fym7E4olpIqqZZhhoFJoaKVvV17lWQoXYwgWN2nF5crA8J2jw==
-- 
GitLab