Flutter/Dart slow unit tests: each file taking >4s to start running - unit-testing

My one test file with 7 empty tests is taking 5+ seconds to start running. I've used both flutter test and the Dart test runner in VS Code, and they both take about the same amount of time.
My whole test suite of ~150 tests takes over 30 seconds to complete, because each test file is taking 3-7 seconds just to start running.
launch.json (for Dart test runner)
{
"name": "Dart: Run all tests",
"type": "dart",
"request": "launch",
"program": "./test/"
},
Timed test run:
00:05 +1 -7: Some tests failed.
real 0m9.532s
user 0m0.075s
sys 0m0.138s
I am using nested group blocks, but I don't believe this should cause such an enormous delay that makes TDD extremely slow
Example section of code
void main() {
group('AuthCubit:', () {
late AuthCubit authCubit;
late MockLogIn logIn;
late MockLogOut logOut;
late MockRestoreSession restoreSession;
setUp(() {
logIn = MockLogIn();
logOut = MockLogOut();
restoreSession= MockRestoreSession();
authCubit = AuthCubit(
logIn : logIn,
logOut : logOut,
restoreSession: restoreSession,
);
});
tearDown(() {
authCubit.close();
});
test('initial state is unauthenticated', () async {
expect(authCubit.state, equals(const AuthStateUnauthenticated()));
});
group('logIn():', () {
//? Create late variables that all stages use, if needed
setUp(() {});
//? Create subgroups for stages this function will step through
group('On failure:', () {
setUp(() {});
test('emits unauthenticated state', () async { throw UnimplementedError(); });
});
group('On success:', () {
setUp(() {});
test('emits authenticated state', () async { throw UnimplementedError(); });
});
});
});
// ... + similar empty tests for other functions ...
}
I've tried even testing two empty example test files both separately and combined (see below code) both take 4+ seconds to run individually. If I combine both test files into one file, the execution time is the almost the same as just one test.
The problem seems to be around launching each test file, not the tests themselves.
Tests
test_test_1.dart
import 'package:flutter_test/flutter_test.dart';
//? Create Mocks, Fakes, etc, if needed
void main() {
group('Test File 1:', () {
//? Create late variables that all functions use, if needed
test('empty test 1', () async { throw UnimplementedError(); });
test('empty test 2', () async { throw UnimplementedError(); });
test('empty test 3', () async { throw UnimplementedError(); });
test('empty test 4', () async { throw UnimplementedError(); });
test('empty test 5', () async { throw UnimplementedError(); });
});
}
test_test_2.dart
import 'package:flutter_test/flutter_test.dart';
//? Create Mocks, Fakes, etc, if needed
void main() {
group('Test File 2:', () {
//? Create late variables that all functions use, if needed
test('empty test 1', () async { throw UnimplementedError(); });
test('empty test 2', () async { throw UnimplementedError(); });
test('empty test 3', () async { throw UnimplementedError(); });
test('empty test 4', () async { throw UnimplementedError(); });
test('empty test 5', () async { throw UnimplementedError(); });
});
}
test_test_1_2.dart
import 'package:flutter_test/flutter_test.dart';
//? Create Mocks, Fakes, etc, if needed
void main() {
group('Test File 1:', () {
//? Create late variables that all functions use, if needed
test('empty test 1', () async { throw UnimplementedError(); });
test('empty test 2', () async { throw UnimplementedError(); });
test('empty test 3', () async { throw UnimplementedError(); });
test('empty test 4', () async { throw UnimplementedError(); });
test('empty test 5', () async { throw UnimplementedError(); });
});
group('Test File 2:', () {
//? Create late variables that all functions use, if needed
test('empty test 1', () async { throw UnimplementedError(); });
test('empty test 2', () async { throw UnimplementedError(); });
test('empty test 3', () async { throw UnimplementedError(); });
test('empty test 4', () async { throw UnimplementedError(); });
test('empty test 5', () async { throw UnimplementedError(); });
});
}
Results
test_test_1.dart individual result
00:04 +0 -5: Some tests failed.
real 0m8.743s
user 0m0.060s
sys 0m0.167s
`test_test_2.dart` individual result
00:05 +0 -5: Some tests failed.
real 0m8.982s
user 0m0.137s
sys 0m0.106s
`test_test_1_2.dart` individual result
00:04 +0 -10: Some tests failed.
real 0m8.602s << Note this is actually FASTER than the smaller test files
user 0m0.045s << same ^
sys 0m0.200s
All 3 test file results (in one run)
00:08 +0 -20: Some tests failed.
real 0m12.696s
user 0m0.015s << Weirdly, this is the smallest value out of all test cases
sys 0m0.152s
Questions
What could be the issue with my code, setup (software or hardware), with Dart/Flutter, or anything else?
Pretend I know nothing about Flutter, Dart, or VS Code; what questions would help find the potential causes and solutions for this?

jensjoha on GitHub helped me find a workaround to this by creating "nested aggregate test files"
On a higher level note I should probably add that for each file you have tests in, running tests are going to be slower because it has to do a compile, launch a new process etc. Similarly - as we saw - getting to the point where it actually starts doing something with the tests takes quite a while, so running flutter test specificfile.dart (at least for many specific files) isn't great.
As somewhat of a workaround - when you want to run all tests - you could try to create one test file like done in #86722 (comment) and only run that.
Example
// test_group_1_test.dart
import test_file_1_test.dart as testFile1;
import test_file_2_test.dart as testFile2;
// ...
void main() {
group("test file 1", testFile1.main);
group("test file 2", testFile2.main);
// ...
}
// everything_test.dart
import test_group_1_test.dart as testGroup1
import test_group_2_test.dart as testGroup2
// ...
void main() {
group("test group 1", testGroup1.main);
group("test group 2", testGroup2.main);
// ...
}
With this structure, I can run all (or any group of) test files within the same time it takes to run a single test file, which for my case is ~75% faster.

Related

How to run single test case when using 'test.concurrent' in Jest?

Update: I checked the issues of Jest again and found this problem has been fixed in v28.0.1. Update to the latest version solved this question.
I write a test file with multiple test cases, and I know how to use test.only or -t to run a "single test case" - as Jest's document and other answers said.
But I notice that when I use test.concurrent, it seems the test cases in the test file are actually run by Jest, and Jest only ignores the result of other test cases.
For example when I run the test below:
describe('Describe', () => {
test.concurrent('test case 1', async () => {
await console.log(1);
});
test.concurrent('test case 2', async () => {
await console.log(2);
});
test.concurrent('test case 3', async () => {
await console.log(3);
});
test.concurrent('test case 4', async () => {
await console.log(4);
});
});
Run:
jest --verbose "--testNamePattern=^Describe test case 1$" --runTestsByPath cuncurrent.test.ts
All the console.log are executed, but when I use test instead of test.current:
describe('Describe', () => {
test('test case 1', async () => {
await console.log(1);
});
test('test case 2', async () => {
await console.log(2);
});
test('test case 3', async () => {
await console.log(3);
});
test('test case 4', async () => {
await console.log(4);
});
});
Only console.log(1) is executed.
Is there anyway to use test.concurrent and run single test case in a test file?

Using different jest mocks of a class for separate unit tests

I am working with AWS and writing some unit tests for a class that makes use of AWS's Secrets Manger service. For those interested, I'll link the documentation to Secrets Manager here: https://docs.aws.amazon.com/secretsmanager/index.html
On to the question: I am trying to write two unit tests for a class that uses Secrets Manager, specifically the 'getSecretValue' function. The gist of my code is below:
Consumer Function File: secretValueManager.ts
import { SecretsManager } from 'aws-sdk';
const manager = new SecretsManager({});
export default async function retrieveSecretValue(secretId: string) {
try {
const result = await manager.getSecretValue({ secretId });
return {
value: result.SecretString,
};
} catch (err: any) {
throw new Error('encountered an error retrieving secret');
}
};
Spec/Test file: secretValueManager.spec.ts
import retrieveSecretValue from './secretValueManager';
// MOCK #1
jest.mock('aws-sdk', () => {
return {
__esModule: true,
SecretsManager: function (): any {
return {
getSecretValue: function (): any {
return {
promise: jest.fn().mockImplementation(() => ({
SecretString: 'returned signingKey',
})),
};
},
};
},
};
});
// MOCK #2
jest.mock('aws-sdk', () => {
return {
__esModule: true,
SecretsManager: function (): any {
return {
getSecretValue: function (): any {
return {
promise: jest.fn().mockImplementation(() => {
throw new Error('error_message');
}),
};
},
};
},
};
});
describe('secretsManager', () => {
it('should get secret value', async () => {
await expect(retrieveSecretValue('secretid')).resolves.toBe({ value: 'returned signingKey' });
});
it('should re-throw error with custom message', async () => {
await expect(retrieveSecretValue('secret id')).rejects.toThrow('encountered an error retrieving secret');
});
});
As you can see, mock #1 is meant to test the 'try' block with the first individual test, and mock #2 is meant to test the 'catch' block with the second individual test. Obviously, I can't call jest.mock() twice and change the mock on a per test basis.
What I am used to doing is importing functions on their own and making use of the jest function mockImplementationOnce(), but the 'getSecretValue' function is not an exported function and only exists on instances of the 'SecretsManager' class.
I know that mock #1 results in test 1 passing, and that mock #2 results in test 2 passing, but how can I use both of these mocks on a per test basis?

Check if a stubbed getter function has been called with sinon spy

I am using firebase admin and I am trying to write some unit tests for my code.
Since admin is injected in my function I figured I could mock a very simple object like this:
admin = {
get auth () {
return {
updateUser: () => {
return true;
},
createUser: () => {
return true;
},
getUser: () => {
throw Error('no user');
}
};
}
};
Then in a particular test I can stub the functions. Here is what I have done so far:
// stubbed functions
sinon.stub(admin, 'auth').get(() => () => ({
updateUser: () => ({ called: true }),
getUser: () => (userRecord),
createUser: () => ({ called: false })
}));
and those are working fine (I can see with my logs).
However in my test I would also want to check if createUser has been called at all.
I thought I could set up a spy on the createUser function, but so far I can't really get it to work.
Here is what I have been trying (with a bunch of variation always failing):
it.only('should update a user', async () => {
const userRecord = mockData
sinon.stub(admin, 'auth').get(() => () => ({
updateUser: () => ({ called: true }),
getUser: () => (userRecord),
createUser: () => ({ called: false })
}));
const spy = sinon.spy(admin, 'auth', ['get']); // this is not working
const user = await upsertUser(data, firestore, admin);
expect(user).toEqual(data.userDataForAuth); // this one is ok
sinon.assert.calledOnce(spy.get); // this throws an error
});
the bit of code I am trying to test (which is the upsert function is this:
// in my test exisiting user is not null (the stub `getUser` is returning a object
if (existingUser != null) {
try {
await admin.auth().updateUser(uid, userDataForAuth);
return userDataForAuth;
} catch (error) {
console.log('error', error);
throw Error('error updating user');
}
}
I am not even sure this is the best approach, happy to change it if there is a better one!

How to write a unit test with data fetching, that alters data based on respone in vuejs?

I am trying to write a unit test for a function that does an async call, but it doesnt seem to alter the data prop, maybe I am doing something wrong.
Check the code below:
getSomething() {
MyService.getThis().then(
response => {
this.status = true;
}
).catch(error => {})
}
TestCase:
describe('test', () => {
beforeEach(() => {
// To ignore the created hook, but this doesnt work, any idea?
spyOn(CustomerData, 'created');
spyOn(MyService, 'getThis').and.returnValue(Promise.resolve(list));
});
wrapper = shallowMount(MyComponent, {
propsData: {
data: {}
},
});
it('should work', () => {
wrapper.vm.getSomething();
expect(wrapper.vm.status).toBeTruthy();
});
});
}
The status should be true, but it is false, but if I print the value of status in the getSomething() function it is indeed true. I have no idea what the issue can be.
update:
In the test case I wrote
it('should work', async () => {
await wrapper.vm.getSomething();
expect(wrapper.vm.status).toBeTruthy();
}
and this seems to work. Is this a good way to solve it? Would love to hear other solutions.
Also I am very interested if it is possible to ignore the created hook, I havent been able to figure that out yet.
Code that running inside getSomething() is asynchronous. MyService.getThis() returns promise, and its execution takes time, in case if you fetching some data from remote serivce.
So first of all you need to return promise from getSomething()
getSomething() {
return MyService.getThis()
.then(response => { this.status = true; })
.catch(error => {})
}
And inside the test you need to return promise outside, to let jest know that your test is asynchronous.
it('should work', () => {
return wrapper.vm.getSomething().then(() => {
expect(wrapper.vm.status).toBeTruthy();
});
});
Or as you mentioned in the edited part you can use async version:
it('should work', async () => {
await getSomething();
expect(wrapper.vm.status).toBeTruthy();
});

Cannot log after tests are done in jestjs

I have written test cases for signin API using jest. After completing all five test of a test suit jest give me following error in log.
Can any body tell Why it is So and how to fix it?
CODE:(signup.test.ts)
import request from 'supertest';
import { TYPES } from '../src/inversify.types'
import { Application } from '../src/app/Application'
import { container } from '../src/inversify.config'
import dotenv from 'dotenv'
import { RESPONSE_CODE } from '../src/utils/enums/ResponseCode'
import { RESPONSE_MESSAGES } from '../src/utils/enums/ResponseMessages'
import { UserSchema } from '../src/components/user/User';
// import jwt from 'jsonwebtoken';
var application: Application
describe("POST / - SIGNUP endpoint", () => {
// var testusers: any;
//This hook is executed before running all test cases, It will make application instance, make it to listen
// on it on port 3000 and add test document in DB
beforeAll(async () => {
// Make enviroment variables available throughout the application
dotenv.config();
// Getting application instance using iversify container
application = container.get<Application>(TYPES.Application);
// Initialize frontside of application
await application.bootstrap();
// Starting Application server on given port
await application.listen(3000);
});
afterAll(
//This hook is executed after running all test cases and delete test document in database
async () =>{
const res = await UserSchema.deleteMany({ Name: { $in: [ "Test User", "Test" ] } });
// `0` if no docs matched the filter, number of docs deleted otherwise
console.log('---------------------->>>>>>>>>>>>>>>>>>>', (res as any).deletedCount);
}
)
it("Signup for user that don\'t exists", async () => {
const response = await request(application.getServer()).post('/user/signup')
.send({
"Email": JSON.parse(process.env.TEST_USER).Email,
"Name": "Test User",
"Password": process.env.TEST_ACCOUNTS_PASSWORD
})
expect(response.status).toBe(RESPONSE_CODE.CREATED);
expect(JSON.parse(response.text)).toEqual(expect.objectContaining({
Message: RESPONSE_MESSAGES.ADDED_SUCESSFULLY,
Data: expect.objectContaining({
Name: 'Test User',
Country: '',
PhoneNumber: '',
// Password: '$2b$10$nIHLW/SA73XLHoIcND27iuODFAArOvpch6FL/eikKT78qbShAl6ry',
Dob: '',
Role: 'MEMBER',
IsEmailVerified: false,
IsBlocked: 'ACTIVE',
IsTokenSent: false,
twoFAStatus: false,
// _id: '5c812e2715e0711b98260fee',
Email: JSON.parse(process.env.TEST_USER).Email
})
})
);
console.log('*** Signup for user that don\'t exists *** response', response.text, 'response status', response.status);
});
it("Signup for user that exists", async () => {
const response = await request(application.getServer()).post('/user/signup')
.send({
"Email": JSON.parse(process.env.TEST_USER).Email,
"Name": "Test User",
"Password": process.env.TEST_ACCOUNTS_PASSWORD
})
expect(response.status).toBe(RESPONSE_CODE.CONFLICT);
expect(JSON.parse(response.text)).toEqual({
Message: RESPONSE_MESSAGES.ALREADY_EXISTS
})
console.log('*** Signup for user that don\'t exists *** response', response.text, 'response status', response.status);
});
});
Jest did not exit one second after the test run has completed.
This usually means that there are asynchronous operations that weren't
stopped in your tests. Consider running Jest with
--detectOpenHandles to troubleshoot this issue.
Cannot log after tests are done. Did you forget to wait for something
async in your test?
Attempted to log "{ accepted: [ 'unverifiedtestuser#abc.com' ],
rejected: [],
envelopeTime: 621,
messageTime: 867,
messageSize: 906,
response: '250 2.0.0 OK 1551945300 f6sm5442066wrt.87 - gsmtp',
envelope:
{ from: 'abc#gmail.com',
to: [ 'unverifiedtestuser#abc.com' ] },
messageId: '<45468449-b5c8-0d86-9404-d55bb5f4g6a3#gmail.com>' }".
at CustomConsole.log (node_modules/jest-util/build/CustomConsole.js:156:10)
at src/email/MailHandler.ts:2599:17
at transporter.send.args (node_modules/nodemailer/lib/mailer/index.js:226:21)
at connection.send (node_modules/nodemailer/lib/smtp-transport/index.js:247:32)
at callback (node_modules/nodemailer/lib/smtp-connection/index.js:435:13)
at stream._createSendStream (node_modules/nodemailer/lib/smtp-connection/index.js:458:24)
at SMTPConnection._actionSMTPStream (node_modules/nodemailer/lib/smtp-connection/index.js:1481:20)
at SMTPConnection._responseActions.push.str (node_modules/nodemailer/lib/smtp-connection/index.js:968:22)
at SMTPConnection._processResponse (node_modules/nodemailer/lib/smtp-connection/index.js:764:20)
at SMTPConnection._onData (node_modules/nodemailer/lib/smtp-connection/index.js:570:14)
I was using the react-native default test case (see below) when Cannot log after tests are done happened.
it('renders correctly', () => {
renderer.create(<App />);
});
Apparently, the problem was that the test ended but logging was still needed. So I tried to make the callback in the test case async, hoping that the test won't terminate immediately:
it('renders correctly', async () => {
renderer.create(<App />);
});
And it worked. However, I have very little clue what the inner working is.
If you are using async/await type in your code, then this error can occur when you are calling async function without await keyword.
In my case, I have defined a function like this below,
async getStatistics(headers) {
....
....
return response;
}
But I have called this method like getStatistics(headers) instead of await getStatistics(headers).
When I included await, it worked fine and the issue resolved.
In my case while using nodejs + jest + supertest the problem was that when I import app from "./app" to my test file to do some stuff with supertest (request(app)), I actually import with app.listen() , because when I'm exporting app, export takes in account app.listen() too, but we don't need app.listen() in tests and it throws an error
"Cannot log after tests are done.Did you forget to wait for something async in your test?"
Here is all in one file(that's the problem!)
const app = express();
app.use(express.json());
// ROUTES
app.get("/api", (req, res) => {
res.json({ message: "Welcome to Blog API!" });
});
app.use("/api/users", usersRoutes);
app.use("/api/blogs", blogsRouter);
// The server will start only if the connection to database is established
mongoose
.connect(process.env.MONGO_URI!)
.then(() => {
console.log("MongoDB est connecté");
const port = process.env.PORT || 4000;
app.listen(port, () => console.log(`The server is running on port: ${port}`));
})
.catch(err => {
console.log(err);
});
export default app;
To solve this issue I created 2 separate folders:
// 1) app.ts
Where I put all stuff for my const app = express(), routes etc and export app
dotenv.config();
const app = express();
app.use(express.json());
// ROUTES
app.get("/api", (req, res) => {
res.json({ message: "Welcome to Blog API!" });
});
app.use("/api/users", usersRoutes);
app.use("/api/blogs", blogsRouter);
export default app;
// 2) index.ts
Where I put app.listen() and mongoose.connection() and import app
*import mongoose from "mongoose";
import app from "./app";
// The server will start only if the connection to database is established
mongoose
.connect(process.env.MONGO_URI!)
.then(() => {
console.log("MongoDB est connecté");
const port = process.env.PORT || 4000;
app.listen(port, () => console.log(`The server is running on port: ${port}`));
})
.catch(err => {
console.log(err);
});*
For me I needed to add an await before the expect() call also to stop this error (and an async before the test() callback function).
Also caused and fixed Jest not detecting coverage on the lines in the code throwing the error!
test("expect error to be thrown for incorrect request", async () => {
await expect(
// ^ added this
async () => await getData("i-made-this-up")
).rejects.toThrow(
"[API] Not recognised: i-made-this-up"
);
});
getData() returns an Axios call and in this case an error is caught by catch and re-thrown.
const getData = async (id) => {
return await axios
.get(`https://api.com/some/path?id=${id}`)
.then((response) => response.data)
.catch((error) => {
if (error?.response?.data?.message) {
console.error(error) // Triggered the error
throw new Error("[API] " + error.response.data.message);
}
throw error;
});
};
This happened to me because I had an infinite loop while (true). In my case, I was able to add a method for setting the value of the loop based on user input, rather than defaulting to true.
In my case, the error was caused by asynchronous Redis connection still online. Just added afterall method to quit Redis and could see the log again.
Working on Typescript 4.4.2:
test("My Test", done => {
let redisUtil: RedisUtil = new RedisUtil();
let redisClient: Redis = redisUtil.redis_client();
done();
});
afterAll(() => {
redisClient.quit();
});
I solved it with the env variables:
if (process.env.NODE_ENV !== 'test') {
db.init().then(() => {
app.listen(PORT, () => {
console.log('API lista por el puerto ', PORT)
})
}).catch((err) => {
console.error(err)
process.exit(1)
})
} else {
module.export = app
}
I faced same warnings. However the fix is bit weird:
The jest unit test script import a function (which is not export from src/). After I added the export to the function to be tested. The error disappears.
I had a similar issue:
Cannot log after tests are done. Did you forget to wait for something async in your test?
Attempted to log "Warning: You seem to have overlapping act() calls, this is not supported. Be sure to await previous act() calls before making a new one. ".
It was due to a missing static keyword. This code caused the issue:
class MyComponent extends React.Component<Props, State> {
propTypes = {
onDestroy: PropTypes.func,
}
}
It should have been:
class MyComponent extends React.Component<Props, State> {
static propTypes = {
onDestroy: PropTypes.func,
}
}