|
| 1 | +let expect; |
| 2 | +before(async () => { |
| 3 | + const chai = await import('chai'); |
| 4 | + expect = chai.expect; |
| 5 | +}); |
| 6 | +const sinon = require('sinon'); |
| 7 | +const { output } = require('codeceptjs'); |
| 8 | +const ExpectHelper = require('./index'); |
| 9 | + |
| 10 | +describe('ExpectHelper', () => { |
| 11 | + let expectHelper; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + expectHelper = new ExpectHelper(); |
| 15 | + sinon.stub(output, 'step'); |
| 16 | + }); |
| 17 | + |
| 18 | + afterEach(() => { |
| 19 | + sinon.restore(); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should assert equality', () => { |
| 23 | + expectHelper.expectEqual(1, 1); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should fail to assert equality', () => { |
| 27 | + expect(() => expectHelper.expectEqual(1, 2)).to.throw(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should assert inequality', () => { |
| 31 | + expectHelper.expectNotEqual(1, 2); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should fail to assert inequality', () => { |
| 35 | + expect(() => expectHelper.expectNotEqual(1, 1)).to.throw(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should assert deep equality', () => { |
| 39 | + expectHelper.expectDeepEqual({ a: 1 }, { a: 1 }); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should fail to assert deep equality', () => { |
| 43 | + expect(() => expectHelper.expectDeepEqual({ a: 1 }, { a: 2 })).to.throw(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should assert deep inequality', () => { |
| 47 | + expectHelper.expectNotDeepEqual({ a: 1 }, { a: 2 }); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should fail to assert deep inequality', () => { |
| 51 | + expect(() => expectHelper.expectNotDeepEqual({ a: 1 }, { a: 1 })).to.throw(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should assert containment', () => { |
| 55 | + expectHelper.expectContain([1, 2, 3], 2); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should fail to assert containment', () => { |
| 59 | + expect(() => expectHelper.expectContain([1, 2, 3], 4)).to.throw(); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should assert non-containment', () => { |
| 63 | + expectHelper.expectNotContain([1, 2, 3], 4); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should fail to assert non-containment', () => { |
| 67 | + expect(() => expectHelper.expectNotContain([1, 2, 3], 2)).to.throw(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should assert starts with', () => { |
| 71 | + expectHelper.expectStartsWith('hello world', 'hello'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should fail to assert starts with', () => { |
| 75 | + expect(() => expectHelper.expectStartsWith('hello world', 'world')).to.throw(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should assert does not start with', () => { |
| 79 | + expectHelper.expectNotStartsWith('hello world', 'world'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should fail to assert does not start with', () => { |
| 83 | + expect(() => expectHelper.expectNotStartsWith('hello world', 'hello')).to.throw(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should assert ends with', () => { |
| 87 | + expectHelper.expectEndsWith('hello world', 'world'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should fail to assert ends with', () => { |
| 91 | + expect(() => expectHelper.expectEndsWith('hello world', 'hello')).to.throw(); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should assert does not end with', () => { |
| 95 | + expectHelper.expectNotEndsWith('hello world', 'hello'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should fail to assert does not end with', () => { |
| 99 | + expect(() => expectHelper.expectNotEndsWith('hello world', 'world')).to.throw(); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should assert JSON schema', () => { |
| 103 | + const schema = { type: 'object', properties: { a: { type: 'number' } } }; |
| 104 | + expectHelper.expectJsonSchema({ a: 1 }, schema); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should fail to assert JSON schema', () => { |
| 108 | + const schema = { type: 'object', properties: { a: { type: 'number' } } }; |
| 109 | + expect(() => expectHelper.expectJsonSchema({ a: '1' }, schema)).to.throw(); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should assert JSON schema using AJV', () => { |
| 113 | + const schema = { type: 'object', properties: { a: { type: 'number' } } }; |
| 114 | + expectHelper.expectJsonSchemaUsingAJV({ a: 1 }, schema); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should fail to assert JSON schema using AJV', () => { |
| 118 | + const schema = { type: 'object', properties: { a: { type: 'number' } } }; |
| 119 | + expect(() => expectHelper.expectJsonSchemaUsingAJV({ a: '1' }, schema)).to.throw(); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should assert has property', () => { |
| 123 | + expectHelper.expectHasProperty({ a: 1 }, 'a'); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should fail to assert has property', () => { |
| 127 | + expect(() => expectHelper.expectHasProperty({ a: 1 }, 'b')).to.throw(); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should assert has a property', () => { |
| 131 | + expectHelper.expectHasAProperty({ a: 1 }, 'a'); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should fail to assert has a property', () => { |
| 135 | + expect(() => expectHelper.expectHasAProperty({ a: 1 }, 'b')).to.throw(); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should assert type', () => { |
| 139 | + expectHelper.expectToBeA('hello', 'string'); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should fail to assert type', () => { |
| 143 | + expect(() => expectHelper.expectToBeA('hello', 'number')).to.throw(); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should assert type (an)', () => { |
| 147 | + expectHelper.expectToBeAn([], 'array'); |
| 148 | + }); |
| 149 | + |
| 150 | + it('should fail to assert type (an)', () => { |
| 151 | + expect(() => expectHelper.expectToBeAn({}, 'array')).to.throw(); |
| 152 | + }); |
| 153 | + |
| 154 | + it('should assert matches regex', () => { |
| 155 | + expectHelper.expectMatchRegex('hello', /^h/); |
| 156 | + }); |
| 157 | + |
| 158 | + it('should fail to assert matches regex', () => { |
| 159 | + expect(() => expectHelper.expectMatchRegex('hello', /^b/)).to.throw(); |
| 160 | + }); |
| 161 | + |
| 162 | + it('should assert length', () => { |
| 163 | + expectHelper.expectLengthOf([1, 2, 3], 3); |
| 164 | + }); |
| 165 | + |
| 166 | + it('should fail to assert length', () => { |
| 167 | + expect(() => expectHelper.expectLengthOf([1, 2, 3], 2)).to.throw(); |
| 168 | + }); |
| 169 | + |
| 170 | + it('should assert empty', () => { |
| 171 | + expectHelper.expectEmpty([]); |
| 172 | + }); |
| 173 | + |
| 174 | + it('should fail to assert empty', () => { |
| 175 | + expect(() => expectHelper.expectEmpty([1])).to.throw(); |
| 176 | + }); |
| 177 | + |
| 178 | + it('should assert true', () => { |
| 179 | + expectHelper.expectTrue(true); |
| 180 | + }); |
| 181 | + |
| 182 | + it('should fail to assert true', () => { |
| 183 | + expect(() => expectHelper.expectTrue(false)).to.throw(); |
| 184 | + }); |
| 185 | + |
| 186 | + it('should assert false', () => { |
| 187 | + expectHelper.expectFalse(false); |
| 188 | + }); |
| 189 | + |
| 190 | + it('should fail to assert false', () => { |
| 191 | + expect(() => expectHelper.expectFalse(true)).to.throw(); |
| 192 | + }); |
| 193 | + |
| 194 | + it('should assert above', () => { |
| 195 | + expectHelper.expectAbove(10, 5); |
| 196 | + }); |
| 197 | + |
| 198 | + it('should fail to assert above', () => { |
| 199 | + expect(() => expectHelper.expectAbove(5, 10)).to.throw(); |
| 200 | + }); |
| 201 | + |
| 202 | + it('should assert below', () => { |
| 203 | + expectHelper.expectBelow(5, 10); |
| 204 | + }); |
| 205 | + |
| 206 | + it('should fail to assert below', () => { |
| 207 | + expect(() => expectHelper.expectBelow(10, 5)).to.throw(); |
| 208 | + }); |
| 209 | + |
| 210 | + it('should assert length above', () => { |
| 211 | + expectHelper.expectLengthAboveThan([1, 2, 3, 4], 3); |
| 212 | + }); |
| 213 | + |
| 214 | + it('should fail to assert length above', () => { |
| 215 | + expect(() => expectHelper.expectLengthAboveThan([1, 2], 3)).to.throw(); |
| 216 | + }); |
| 217 | + |
| 218 | + it('should assert length below', () => { |
| 219 | + expectHelper.expectLengthBelowThan([1, 2], 3); |
| 220 | + }); |
| 221 | + |
| 222 | + it('should fail to assert length below', () => { |
| 223 | + expect(() => expectHelper.expectLengthBelowThan([1, 2, 3, 4], 3)).to.throw(); |
| 224 | + }); |
| 225 | + |
| 226 | + it('should assert equal ignoring case', () => { |
| 227 | + expectHelper.expectEqualIgnoreCase('hello', 'HELLO'); |
| 228 | + }); |
| 229 | + |
| 230 | + it('should fail to assert equal ignoring case', () => { |
| 231 | + expect(() => expectHelper.expectEqualIgnoreCase('hello', 'WORLD')).to.throw(); |
| 232 | + }); |
| 233 | + |
| 234 | + it('should assert deep members', () => { |
| 235 | + expectHelper.expectDeepMembers([{ a: 1 }], [{ a: 1 }]); |
| 236 | + }); |
| 237 | + |
| 238 | + it('should fail to assert deep members', () => { |
| 239 | + expect(() => expectHelper.expectDeepMembers([{ a: 1 }], [{ a: 2 }])).to.throw(); |
| 240 | + }); |
| 241 | + |
| 242 | + it('should assert deep include members', () => { |
| 243 | + expectHelper.expectDeepIncludeMembers([{ a: 1 }, { b: 2 }], [{ a: 1 }]); |
| 244 | + }); |
| 245 | + |
| 246 | + it('should fail to assert deep include members', () => { |
| 247 | + expect(() => expectHelper.expectDeepIncludeMembers([{ a: 1 }], [{ b: 2 }])).to.throw(); |
| 248 | + }); |
| 249 | + |
| 250 | + it('should assert deep equal excluding fields', () => { |
| 251 | + expectHelper.expectDeepEqualExcluding({ a: 1, b: 2 }, { a: 1, b: 3 }, ['b']); |
| 252 | + }); |
| 253 | + |
| 254 | + it('should fail to assert deep equal excluding fields', () => { |
| 255 | + expect(() => expectHelper.expectDeepEqualExcluding({ a: 1, b: 2 }, { a: 2, b: 3 }, ['b'])).to.throw(); |
| 256 | + }); |
| 257 | + |
| 258 | + it('should assert matches pattern', () => { |
| 259 | + expectHelper.expectMatchesPattern({ a: 1, b: 2 }, { a: 1, b: 2 }); |
| 260 | + }); |
| 261 | + |
| 262 | + it('should fail to assert matches pattern', () => { |
| 263 | + expect(() => expectHelper.expectMatchesPattern({ a: 1, b: 2 }, { a: 2, b: 3 })).to.throw(); |
| 264 | + }); |
| 265 | +}); |
0 commit comments