Skip to content

Commit de07ed4

Browse files
committed
test: Make tests compatible with Jest
1 parent cf5c5b7 commit de07ed4

File tree

5 files changed

+63
-57
lines changed

5 files changed

+63
-57
lines changed

.eslintrc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@
2929
"no-unused-expressions": 0,
3030
},
3131
"env": {
32-
"mocha": true
32+
"jasmine": true,
33+
"jest": true
34+
},
35+
"globals": {
36+
"Class": true,
37+
"$Shape": true,
3338
},
3439
"plugins": [
3540
"flowtype",

src/__tests__/composeWithRelay-test.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* @flow */
22

3-
import { expect } from 'chai';
43
import { TypeComposer, graphql } from 'graphql-compose';
54
import { composeWithRelay } from '../composeWithRelay';
65
import { userTypeComposer } from '../__mocks__/userTypeComposer';
@@ -17,44 +16,45 @@ describe('composeWithRelay', () => {
1716

1817
describe('basic checks', () => {
1918
it('should return TypeComposer', () => {
20-
expect(userComposer).instanceof(TypeComposer);
19+
expect(userComposer).toBeInstanceOf(TypeComposer);
2120
});
2221

2322
it('should throw error if got a not TypeComposer', () => {
24-
expect(() => composeWithRelay(123)).to.throw('should provide TypeComposer instance');
23+
expect(() => composeWithRelay(123)).toThrowError('should provide TypeComposer instance');
2524
});
2625

2726
it('should throw error if TypeComposer without recordIdFn', () => {
2827
const tc = userTypeComposer.clone('AnotherUserType2');
2928
delete tc.gqType._gqcGetRecordIdFn;
30-
expect(() => composeWithRelay(tc)).to.throw('should have recordIdFn');
29+
expect(() => composeWithRelay(tc)).toThrowError('should have recordIdFn');
3130
});
3231

3332
it('should thow error if typeComposer does not have findById resolver', () => {
3433
const tc = userTypeComposer.clone('AnotherUserType');
3534
tc.removeResolver('findById');
36-
expect(() => composeWithRelay(tc)).to.throw("does not have resolver with name 'findById'");
35+
expect(() => composeWithRelay(tc)).toThrowError(
36+
"does not have resolver with name 'findById'"
37+
);
3738
});
3839
});
3940

4041
describe('when pass RootQuery type composer', () => {
4142
it('should add `node` field to RootQuery', () => {
4243
const nodeField = rootQueryComposer.getField('node');
43-
expect(nodeField).to.be.ok;
44-
expect(nodeField).property('type').instanceof(GraphQLInterfaceType);
45-
expect(nodeField).nested.property('type.name').to.equal('Node');
44+
expect(nodeField.type).toBeInstanceOf(GraphQLInterfaceType);
45+
expect(nodeField.type.name).toBe('Node');
4646
});
4747
});
4848

4949
describe('when pass User type composer (not RootQuery)', () => {
5050
it('should add or override id field', () => {
5151
const idField = userComposer.getField('id');
52-
expect(idField.description).to.contain('globally unique ID');
52+
expect(idField.description).toContain('globally unique ID');
5353
});
5454

5555
it('should make id field NonNull', () => {
5656
const idField = userComposer.getField('id');
57-
expect(idField.type).instanceof(GraphQLNonNull);
57+
expect(idField.type).toBeInstanceOf(GraphQLNonNull);
5858
});
5959

6060
it('should resolve globalId in `user.id` field', async () => {
@@ -72,8 +72,8 @@ describe('composeWithRelay', () => {
7272
}
7373
}`;
7474
const result = await graphql.graphql(schema, query);
75-
expect(result).nested.property('data.user.id').equal(toGlobalId('User', 1));
76-
expect(result).nested.property('data.user.name').equal('Pavel');
75+
expect(result.data.user.id).toBe(toGlobalId('User', 1));
76+
expect(result.data.user.name).toBe('Pavel');
7777
});
7878

7979
it('should resolve globalId in `node.id` field', async () => {
@@ -94,8 +94,8 @@ describe('composeWithRelay', () => {
9494
name
9595
}`;
9696
const result = await graphql.graphql(schema, query);
97-
expect(result).nested.property('data.node.id').equal(toGlobalId('User', 1));
98-
expect(result).nested.property('data.node.name').equal('Pavel');
97+
expect(result.data.node.id).toBe(toGlobalId('User', 1));
98+
expect(result.data.node.name).toBe('Pavel');
9999
});
100100

101101
it('should passthru clientMutationId in mutations', async () => {
@@ -116,8 +116,8 @@ describe('composeWithRelay', () => {
116116
}
117117
}`;
118118
const result = await graphql.graphql(schema, query);
119-
expect(result).nested.property('data.createUser.record.name').equal('Ok');
120-
expect(result).nested.property('data.createUser.clientMutationId').equal('123');
119+
expect(result.data.createUser.record.name).toBe('Ok');
120+
expect(result.data.createUser.clientMutationId).toBe('123');
121121
});
122122
});
123123
});

src/__tests__/globalId-test.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
/* @flow */
22

3-
import { expect } from 'chai';
43
import { base64, unbase64, toGlobalId, fromGlobalId } from '../globalId';
54

65
describe('globalId', () => {
76
it('should have correct method base64()', () => {
8-
expect(base64('123')).to.equal('MTIz');
9-
expect(base64('lksdnfkksdknsdc:123')).to.equal('bGtzZG5ma2tzZGtuc2RjOjEyMw==');
7+
expect(base64('123')).toBe('MTIz');
8+
expect(base64('lksdnfkksdknsdc:123')).toBe('bGtzZG5ma2tzZGtuc2RjOjEyMw==');
109
});
1110

1211
it('should have correct method unbase64()', () => {
13-
expect(unbase64('MTIz')).to.equal('123');
14-
expect(unbase64('bGtzZG5ma2tzZGtuc2RjOjEyMw==')).to.equal('lksdnfkksdknsdc:123');
12+
expect(unbase64('MTIz')).toBe('123');
13+
expect(unbase64('bGtzZG5ma2tzZGtuc2RjOjEyMw==')).toBe('lksdnfkksdknsdc:123');
1514
});
1615

1716
it('should have correct method toGlobalId()', () => {
18-
expect(toGlobalId('User', '789')).to.equal('VXNlcjo3ODk=');
19-
expect(toGlobalId('Article', 22)).to.equal('QXJ0aWNsZToyMg==');
17+
expect(toGlobalId('User', '789')).toBe('VXNlcjo3ODk=');
18+
expect(toGlobalId('Article', 22)).toBe('QXJ0aWNsZToyMg==');
2019
});
2120

2221
it('should have correct method fromGlobalId()', () => {
23-
expect(fromGlobalId('VXNlcjo3ODk=')).to.deep.equal({
22+
expect(fromGlobalId('VXNlcjo3ODk=')).toEqual({
2423
type: 'User',
2524
id: '789',
2625
});
27-
expect(fromGlobalId('QXJ0aWNsZToyMg==')).to.deep.equal({
26+
expect(fromGlobalId('QXJ0aWNsZToyMg==')).toEqual({
2827
type: 'Article',
2928
id: '22',
3029
});
Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* @flow */
22

3-
import { expect } from 'chai';
43
import { graphql } from 'graphql-compose';
54
import { findByIdResolver, userTypeComposer } from '../__mocks__/userTypeComposer';
65
import { toGlobalId } from '../globalId';
@@ -18,33 +17,35 @@ describe('nodeFieldConfig', () => {
1817
const config = getNodeFieldConfig(typeToFindByIdMap);
1918

2019
it('should have type GraphQLInterfaceType', () => {
21-
expect(config).to.be.ok;
22-
expect(config).property('type').instanceof(GraphQLInterfaceType);
23-
expect(config).nested.property('type.name').to.equal('Node');
20+
expect(config).toBeTruthy();
21+
expect(config.type).toBeInstanceOf(GraphQLInterfaceType);
22+
expect(config.type.name).toBe('Node');
2423
});
2524

2625
it('should have args with id', () => {
27-
expect(config).nested.property('args.id.type').instanceof(GraphQLNonNull);
26+
expect(config.args.id.type).toBeInstanceOf(GraphQLNonNull);
2827
});
2928

3029
it('should have resolve function', () => {
31-
expect(config).respondTo('resolve');
30+
expect(config.resolve).toBeDefined();
31+
expect(config.resolve.call).toBeDefined();
32+
expect(config.resolve.apply).toBeDefined();
3233
});
3334

3435
it('should return null if args.id not defined', () => {
35-
expect(config.resolve({}, {})).to.be.null;
36+
expect(config.resolve({}, {})).toBeNull();
3637
});
3738

3839
it('should return null if findById not defined for type', () => {
39-
expect(config.resolve({}, { id: toGlobalId('UnexistedType', 1) })).to.be.null;
40+
expect(config.resolve({}, { id: toGlobalId('UnexistedType', 1) })).toBeNull();
4041
});
4142

4243
it('should return Promise if type exists, but id not exist', () => {
43-
expect(config.resolve({}, { id: toGlobalId('User', 666) })).instanceof(Promise);
44+
expect(config.resolve({}, { id: toGlobalId('User', 666) })).toBeInstanceOf(Promise);
4445
});
4546

4647
it('should return Promise with user data', async () => {
4748
const res = await config.resolve({}, { id: toGlobalId('User', 1) });
48-
expect(res).property('name').to.equal('Pavel');
49+
expect(res.name).toBe('Pavel');
4950
});
5051
});
Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* @flow */
22

3-
import { expect } from 'chai';
43
import { TypeComposer, InputTypeComposer, graphql } from 'graphql-compose';
54
import { composeWithRelay } from '../composeWithRelay';
65
import { userTypeComposer } from '../__mocks__/userTypeComposer';
@@ -21,62 +20,64 @@ describe('wrapMutationResolver', () => {
2120
describe('args', () => {
2221
it('should add `clientMutationId` field to args.input', () => {
2322
const itc = new InputTypeComposer(fieldConfig.args.input.type);
24-
expect(itc.hasField('clientMutationId')).to.be.true;
25-
expect(itc.getFieldType('clientMutationId')).equal(GraphQLString);
23+
expect(itc.hasField('clientMutationId')).toBe(true);
24+
expect(itc.getFieldType('clientMutationId')).toBe(GraphQLString);
2625
});
2726

2827
it('should create required args.input! if not exists', () => {
29-
expect(fieldConfigManyArgsWithoutInput.args).property('input').to.be.ok;
28+
expect(fieldConfigManyArgsWithoutInput.args.input).toBeTruthy();
3029

31-
expect(fieldConfigManyArgsWithoutInput.args).nested
32-
.property('input.type')
33-
.instanceof(GraphQLNonNull);
30+
expect(fieldConfigManyArgsWithoutInput.args.input.type).toBeInstanceOf(GraphQLNonNull);
3431
});
3532

3633
it('should create args.input if not exists and move all args into it', () => {
37-
expect(fieldConfigManyArgsWithoutInput.args).to.have.all.keys(['input']);
34+
expect(Object.keys(fieldConfigManyArgsWithoutInput.args)).toEqual(
35+
expect.arrayContaining(['input'])
36+
);
3837

3938
const type = getNamedType(fieldConfigManyArgsWithoutInput.args.input.type);
4039
const itc = new InputTypeComposer(type);
41-
expect(itc.hasField('sort')).to.be.true;
42-
expect(itc.hasField('limit')).to.be.true;
43-
expect(itc.hasField('clientMutationId')).to.be.true;
40+
expect(itc.hasField('sort')).toBe(true);
41+
expect(itc.hasField('limit')).toBe(true);
42+
expect(itc.hasField('clientMutationId')).toBe(true);
4443
});
4544

4645
it('should leave other arg untouched if args.input exists', () => {
47-
expect(fieldConfigManyArgsWithInput.args).to.have.all.keys(['input', 'sort', 'limit']);
46+
expect(Object.keys(fieldConfigManyArgsWithInput.args)).toEqual(
47+
expect.arrayContaining(['input', 'sort', 'limit'])
48+
);
4849

4950
const type = getNamedType(fieldConfigManyArgsWithInput.args.input.type);
5051
const itc = new InputTypeComposer(type);
51-
expect(itc.hasField('sort')).to.be.false;
52-
expect(itc.hasField('limit')).to.be.false;
53-
expect(itc.hasField('clientMutationId')).to.be.true;
52+
expect(itc.hasField('sort')).toBe(false);
53+
expect(itc.hasField('limit')).toBe(false);
54+
expect(itc.hasField('clientMutationId')).toBe(true);
5455
});
5556
});
5657

5758
describe('outputType', () => {
5859
it('should add `clientMutationId` field to payload', () => {
5960
const tc = new TypeComposer(fieldConfig.type);
60-
expect(tc.hasField('clientMutationId')).to.be.true;
61-
expect(tc.getFieldType('clientMutationId')).equal(GraphQLString);
61+
expect(tc.hasField('clientMutationId')).toBe(true);
62+
expect(tc.getFieldType('clientMutationId')).toBe(GraphQLString);
6263
});
6364

6465
it('should add `nodeId` field to payload', () => {
6566
const tc = new TypeComposer(fieldConfig.type);
66-
expect(tc.hasField('nodeId')).to.be.true;
67-
expect(tc.getFieldType('nodeId')).equal(GraphQLID);
67+
expect(tc.hasField('nodeId')).toBe(true);
68+
expect(tc.getFieldType('nodeId')).toBe(GraphQLID);
6869
});
6970
});
7071

7172
describe('resolve', () => {
7273
it('should passthru `clientMutationId`', async () => {
7374
const result = await fieldConfig.resolve({}, { input: { clientMutationId: '333' } });
74-
expect(result).property('clientMutationId').equal('333');
75+
expect(result.clientMutationId).toBe('333');
7576
});
7677

7778
it('should return `nodeId` with globalId', async () => {
7879
const result = await fieldConfig.resolve({}, { input: { id: 'newRecord' } });
79-
expect(result).property('nodeId').equal(toGlobalId('User', 'newRecord'));
80+
expect(result.nodeId).toBe(toGlobalId('User', 'newRecord'));
8081
});
8182
});
8283
});

0 commit comments

Comments
 (0)