Skip to content

Commit 85b18b6

Browse files
committed
helloworld: Fix test
1 parent f604335 commit 85b18b6

4 files changed

Lines changed: 28 additions & 18 deletions

File tree

helloworld/HelloWorld.test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
/* eslint-disable import/imports-first, import/first */
22

3-
import crypto from 'crypto';
43
import type {MockedStateInterface} from '../lib/__mocks__/state';
54
import Slack from '../lib/slackMock';
65
import State from '../lib/state';
76
import {HelloWorld, type StateObj} from './HelloWorld';
87

8+
const randomUUID = vi.hoisted(() => vi.fn(() => 'test-uuid'));
9+
910
vi.mock('../lib/slackUtils');
1011
vi.mock('../lib/state');
1112
vi.mock('os', () => ({
1213
hostname: vi.fn(() => 'test-hostname'),
1314
release: vi.fn(() => 'test-release'),
1415
}));
1516
vi.mock('crypto', () => ({
16-
randomUUID: vi.fn(() => 'test-uuid'),
17+
default: {randomUUID},
18+
randomUUID,
1719
}));
1820

1921
const MockedState = State as MockedStateInterface<StateObj>;
20-
const mockedCrypto = vi.mocked(crypto);
2122

2223
describe('helloworld', () => {
2324
let slack: Slack = null;
@@ -33,8 +34,8 @@ describe('helloworld', () => {
3334
});
3435

3536
it('initializes correctly', () => {
36-
expect(mockedCrypto.randomUUID).toHaveBeenCalledTimes(1);
37-
expect(mockedCrypto.randomUUID.mock.calls[0]).toHaveLength(0);
37+
expect(randomUUID).toHaveBeenCalledTimes(1);
38+
expect(randomUUID.mock.calls[0]).toHaveLength(0);
3839

3940
const state = MockedState.mocks.get('helloworld');
4041
expect(state.counter).toBe(0);

lib/logger.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import winston from 'winston';
33
import {Syslog as WinstonSyslog} from 'winston-syslog';
44
import {inspect} from 'util';
55
import type {FastifyLogFn} from 'fastify';
6-
import pinoStdSerializers from 'pino-std-serializers';
7-
const serializeHttpResponse = pinoStdSerializers.res;
6+
import {res as serializeHttpResponse} from 'pino-std-serializers';
87

98
const logger = winston.createLogger({
109
level: 'info',

mahjong/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ const fs = require('fs');
33
const path = require('path');
44
const qs = require('querystring');
55
const {promisify} = require('util');
6-
const {Mutex} = require('async-mutex');
76
const {v2: cloudinary} = require('cloudinary');
87
const {source} = require('common-tags');
98
const {chunk, shuffle, sampleSize, sample, random, range, zip} = require('lodash');
109
const {unlock, increment} = require('../achievements');
1110
const {AteQuiz} = require('../atequiz/index');
1211
const {blockDeploy} = require('../deploy/index');
12+
const {Mutex} = require('async-mutex');
1313
const calculator = require('./calculator.js');
1414

1515
const mutex = new Mutex();
@@ -987,7 +987,9 @@ module.exports = (clients) => {
987987
{username: 'mahjong', icon_emoji: ':mahjong:'},
988988
);
989989

990-
const result = await mutex.runExclusive(async () => ateQuiz.start());
990+
const result = await mutex.runExclusive(async () => {
991+
return ateQuiz.start();
992+
});
991993

992994
if (result.state === 'solved') {
993995
await increment(result.correctAnswerer, 'mahjong-chinitsu-quiz-answer');

room-gacha/index.test.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
import roomGacha from './index';
22
import Slack from '../lib/slackMock';
3+
import axios from 'axios';
4+
import type { AxiosResponse } from 'axios';
5+
import { promises as fs } from 'fs';
36
import { stripIndents } from 'common-tags';
47
import assert from 'assert';
58
import type { KnownBlock } from '@slack/web-api';
9+
import type { ScrapeOptions } from 'scrape-it';
610

7-
vi.mock('scrape-it', async (importOriginal) => {
8-
const actual = await importOriginal<typeof import('scrape-it')>();
9-
const {scrapeHTML} = actual;
11+
vi.mock('axios');
12+
13+
// scrape-itからのaxiosの呼び出しはモックできないため、簡易実装を提供する
14+
vi.mock('scrape-it', async () => {
15+
const scrapeIt = await vi.importActual<typeof import('scrape-it')>('scrape-it');
16+
const cheerio = await vi.importActual<typeof import('cheerio')>('cheerio');
1017
return {
11-
default: vi.fn(async (_url: unknown, opts: Parameters<typeof scrapeHTML>[1]) => {
12-
const {promises: fs} = await import('fs');
13-
const html = await fs.readFile(`${__dirname}/search-result.test.html`, 'utf-8');
14-
return {data: scrapeHTML(html, opts)};
15-
}),
16-
scrapeHTML,
18+
default: async (url: string, opts: ScrapeOptions) => {
19+
const res = await axios(url);
20+
return {data: scrapeIt.scrapeHTML(cheerio.load(res.data), opts)};
21+
},
1722
};
1823
});
1924

@@ -27,6 +32,9 @@ beforeEach(async () => {
2732

2833
describe('room-gacha', () => {
2934
it('responds to "物件ガチャ" with a prefecture and a city specified', async () => {
35+
const data = await fs.readFile(`${__dirname}/search-result.test.html`, 'utf-8');
36+
const mockAxios = vi.mocked(axios);
37+
mockAxios.mockResolvedValue({data} as AxiosResponse);
3038
const response = await slack.getResponseTo('物件ガチャ 東京都 文京区');
3139
const blocks = 'blocks' in response ? response.blocks : [];
3240
expect('username' in response && response.username).toBe('物件ガチャ');

0 commit comments

Comments
 (0)