From 862267a8398ad2dfb035852f7fb95b8a913d9cbe Mon Sep 17 00:00:00 2001 From: Koki Takahashi Date: Tue, 26 Nov 2024 20:40:17 +0000 Subject: [PATCH] Make unit tests pass in codespaces --- .devcontainer/devcontainer.json | 5 ++++- .vscode/launch.json | 5 ++++- __mocks__/fs.js | 12 ++++++++++++ auto-archiver/index.test.ts | 4 ++++ shogi/index.test.js | 10 ++++++++++ 5 files changed, 34 insertions(+), 2 deletions(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 09bf9445..6c18da32 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -25,11 +25,14 @@ "settings": { "files.autoSave": "off", "editor.formatOnSave": true, + "editor.defaultFormatter": "dbaeumer.vscode-eslint", "editor.codeActionsOnSave": { "source.fixAll": "always", "source.fixAll.eslint": "always" }, - "eslint.format.enable": true + "eslint.format.enable": true, + "typescript.format.enable": false, + "javascript.format.enable": false }, "extensions": [ "ms-vscode.vscode-typescript-next", diff --git a/.vscode/launch.json b/.vscode/launch.json index ca2f6750..2dc492b4 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -36,7 +36,10 @@ "/**" ], "console": "internalConsole", - "outputCapture": "std" + "outputCapture": "std", + "env": { + "NODE_OPTIONS": "--max-old-space-size=4096" + } } ], "inputs": [ diff --git a/__mocks__/fs.js b/__mocks__/fs.js index da1100c0..5b45823b 100644 --- a/__mocks__/fs.js +++ b/__mocks__/fs.js @@ -43,6 +43,18 @@ fs.promises.readFile = jest.fn((...args) => { } }); +fs.promises.readdir = jest.fn((...args) => { + const [path] = args; + const fullPath = Path.resolve(process.cwd(), path); + const files = Object.keys(fs.virtualFiles).filter((file) => file.startsWith(fullPath)); + + if (files.length > 0) { + return new Promise((resolve) => resolve(files.map((file) => Path.basename(file)))); + } else { + return realFs.promises.readdir(...args); + } +}); + fs.access = jest.fn((...args) => { const [path, , callback] = args; const fullPath = Path.resolve(process.cwd(), path); diff --git a/auto-archiver/index.test.ts b/auto-archiver/index.test.ts index 89e9acfb..05888ae7 100644 --- a/auto-archiver/index.test.ts +++ b/auto-archiver/index.test.ts @@ -160,6 +160,9 @@ describe('auto-archiver', () => { }); it('should archive channel if user responded with "stop"', async () => { + const FAKE_TOKEN = 'xoxt-1234-5678-91011'; + process.env.HAKATASHI_TOKEN = FAKE_TOKEN; + const slack = new Slack(); const mockedAction = slack.messageClient.action as jest.MockedFunction; @@ -226,6 +229,7 @@ describe('auto-archiver', () => { expect((blocks[1] as SectionBlock).text.text).toBe('<@U12345678>の回答: *使用しない*'); expect(slack.webClient.conversations.archive).toBeCalledWith({ channel: FAKE_CHANNEL, + token: FAKE_TOKEN, }); }); diff --git a/shogi/index.test.js b/shogi/index.test.js index 706ab2f7..9a49294b 100644 --- a/shogi/index.test.js +++ b/shogi/index.test.js @@ -2,12 +2,22 @@ jest.mock('cloudinary'); jest.mock('sqlite'); +jest.mock('sqlite3', () => ({ + Database: jest.fn(), +})); +jest.mock('fs'); +const fs = require('fs'); +const path = require('path'); const cloudinary = require('cloudinary'); const sqlite = require('sqlite'); const {default: Slack} = require('../lib/slackMock.ts'); const shogi = require('./index.js'); +fs.virtualFiles = { + [path.join(__dirname, 'boards', 'hoge.sqlite3')]: '', +}; + let slack = null; beforeEach(() => {