Skip to content

Commit

Permalink
Improve testing by checking a file is created wit hthe correct value …
Browse files Browse the repository at this point in the history
…as opposed to checking a function waw called
  • Loading branch information
Relequestual committed Jun 21, 2024
1 parent 3dd0683 commit 8f8b2cb
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 30 deletions.
9 changes: 4 additions & 5 deletions projects/initial-data/jest.setup.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/* eslint-disable no-undef */
import { server } from './mocks/server';
import { vol } from 'memfs';
beforeAll(() => server.listen());
beforeEach(() => {
vol.reset();

beforeAll(() => {
server.listen();
});
afterEach(() => server.resetHandlers());
afterAll(() => server.close());
afterAll(() => server.close());
22 changes: 10 additions & 12 deletions projects/initial-data/mocks/handlers.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { http, HttpResponse } from 'msw';

export const releaseDateHandler =
http.get('https://api.github.com/*', () => {
return HttpResponse.json({
'repository': 'octocat/hello-world',
'names': 'python, json, json-schema, docker, postgresql, frontend, backend, fastapi, traefik, letsencrypt, swagger, jwt, openapi, chakra-ui, react, tanstack-query, tanstack-router, typescript, sqlmodel',
'date_first_commit': '2019-02-09T15:42:36Z',
'created_at': '2019-02-23T15:08:34Z',
'date_first_release': '2019-03-11T09:49:01Z',
});
}
);
export const releaseDateHandler = http.get('https://api.github.com/*', () => {
return HttpResponse.json({
repository: 'octocat/hello-world',
names: 'python, json, json-schema, docker, postgresql',
date_first_commit: '2019-02-09T15:42:36Z',
created_at: '2019-02-23T15:08:34Z',
date_first_release: '2019-03-11T09:49:01Z',
});
});

export const handlers = [releaseDateHandler];
export const handlers = [releaseDateHandler];
62 changes: 49 additions & 13 deletions projects/initial-data/processRespository.test.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,67 @@
/* eslint-disable no-undef */
import { tmpdir } from 'node:os';
import { sep } from 'node:path';
import fs from 'fs';

import { fetchRepoCreationDate } from './main.js';
import { getInput } from './setup.js';
import { Octokit } from 'octokit';
import { DataRecorder } from './dataRecorder.js';
import fs from 'fs';
import { describe, it, expect, afterEach, afterAll } from '@jest/globals';

const { token } = getInput();
const octokit = new Octokit({ auth: token });
const tmpDir = tmpdir();
const tmpDirForTests = fs.mkdtempSync(`${tmpDir}${sep}`);

describe('Basic tests', () => {
afterEach((done) => {
fs.rm(tmpDirForTests, { recursive: true, force: true }, (err) => {
if (err) {
console.log(err);
done(err);
} else {
console.debug('temp directory cleared');
fs.mkdir(tmpDirForTests, {}, (err) => {
if (err) {
console.log(err);
done(err);
} else {
done();
}
});
}
});
});

describe('Skretch Objective', () => {
let data;
it('receives a mocked response from the GitHub API', async () => {
afterAll((done) => {
fs.rm(tmpDirForTests, { recursive: true, force: true }, (err) => {
if (err) {
throw err;
}

console.log(`${tmpDirForTests} is deleted!`);
done();
});
});

it('Simple use of Octokit calls the GitHub API', async () => {
let data;
data = await fetchRepoCreationDate(octokit, 'octocat', 'hello-world');
expect(data).toEqual(1550934514000);


});

it('writes JSON data to csv file', async() => {
it('DataRecorder writes JSON data to csv file', async () => {
const response = {
'date_first_commit': 1550934514000,
date_first_commit: 1550934514000,
};
const spy = jest.spyOn(DataRecorder.prototype, 'appendToCSV').mockImplementation(() => fs.appendFileSync('test.csv', '1550934514000', 'utf8'));
const dataRecorder = new DataRecorder('test.csv');
const fileName = `${tmpDirForTests}${sep}test.csv`;

const dataRecorder = new DataRecorder(fileName);
dataRecorder.appendToCSV([response.date_first_commit]);
const fileContent = fs.readFileSync(fileName, 'utf8');

expect(spy).toHaveBeenCalled();
expect(spy).toHaveBeenCalledWith([data]);
expect(fileContent).toEqual(
expect.stringContaining(response.date_first_commit.toString()),
);
});
});

0 comments on commit 8f8b2cb

Please sign in to comment.