Use expect's instanceOf and toHaveProperty methods

This commit is contained in:
Deven Bansod 2018-03-27 21:59:38 +05:30
parent c9b99af5a6
commit b09c729675
51 changed files with 101 additions and 124 deletions

View File

@ -22,6 +22,7 @@
"files": [
"packages/*/test/**/*.js",
"codemods/*/test/**/*.js",
"packages/babel-helper-transform-fixture-test-runner/src/helpers.js",
"test/**/*.js"
],
"env": {

View File

@ -1,13 +1,7 @@
import assert from "assert";
export function assertNoOwnProperties(obj) {
assert.equal(Object.getOwnPropertyNames(obj).length, 0);
expect(Object.getOwnPropertyNames(obj)).toHaveLength(0);
}
export function assertHasOwnProperty() {}
export function assertLacksOwnProperty() {}
export function multiline(arr) {
return arr.join("\n");
}

View File

@ -58,7 +58,7 @@ class Example {
}
}
expect(Example.prototype.hasOwnProperty('decoratedProps'));
expect(Example.prototype).toHaveProperty('decoratedProps');
expect(Example.prototype.decoratedProps).toEqual([
"enumconfwrite",
"enumconf",

View File

@ -59,7 +59,7 @@ class Example {
}
expect(Example.prototype.hasOwnProperty('decoratedProps'));
expect(Example.prototype).toHaveProperty('decoratedProps');
expect(Example.prototype.decoratedProps).toEqual([
"enumconfwrite",
"enumconf",

View File

@ -44,7 +44,7 @@ class Example {
const inst = new Example();
expect(Example.prototype.hasOwnProperty("decoratedProps")).toBeTruthy();
expect(Example.prototype).toHaveProperty("decoratedProps");
expect(inst.decoratedProps).toEqual([
"enumconfwrite",
"enumconf",

View File

@ -7,5 +7,5 @@ class Example {
}
let inst = new Example();
expect(inst.hasOwnProperty("prop")).toBe(true);
expect(inst).toHaveProperty("prop");
expect(inst.prop).toBeUndefined();

View File

@ -44,7 +44,7 @@ class Example {
const inst = new Example();
expect(Example.prototype.hasOwnProperty("decoratedProps")).toBeTruthy();
expect(Example.prototype).toHaveProperty("decoratedProps");
expect(inst.decoratedProps).toEqual([
"enumconfwrite",
"enumconf",

View File

@ -58,7 +58,7 @@ class Example {
}
}
expect(Example.hasOwnProperty("decoratedProps")).toBeTruthy();
expect(Example).toHaveProperty("decoratedProps");
expect(Example.decoratedProps).toEqual([
"enumconfwrite",
"enumconf",

View File

@ -59,7 +59,7 @@ class Example {
}
expect(Example.hasOwnProperty("decoratedProps"));
expect(Example).toHaveProperty("decoratedProps");
expect(Example.decoratedProps).toEqual([
"enumconfwrite",
"enumconf",

View File

@ -44,7 +44,7 @@ class Example {
const inst = new Example();
expect(Example.hasOwnProperty("decoratedProps")).toBeTruthy();
expect(Example).toHaveProperty("decoratedProps");
expect(Example.decoratedProps).toEqual([
"enumconfwrite",
"enumconf",

View File

@ -6,5 +6,5 @@ class Example {
@dec static prop;
}
expect(Example.hasOwnProperty("prop")).toBeTruthy();
expect(Example).toHaveProperty("prop");
expect(Example.prop).toBe(undefined);

View File

@ -44,7 +44,7 @@ class Example {
const inst = new Example();
expect(Example.hasOwnProperty("decoratedProps")).toBeTruthy();
expect(Example).toHaveProperty("decoratedProps");
expect(Example.decoratedProps).toEqual([
"enumconfwrite",
"enumconf",

View File

@ -58,7 +58,7 @@ const inst = {
},
}
expect(inst.hasOwnProperty('decoratedProps')).toBeTruthy();
expect(inst).toHaveProperty('decoratedProps');
expect(inst.decoratedProps).toEqual([
"enumconfwrite",
"enumconf",

View File

@ -58,7 +58,7 @@ const inst = {
},
}
expect(inst.hasOwnProperty('decoratedProps')).toBeTruthy();
expect(inst).toHaveProperty('decoratedProps');
expect(inst.decoratedProps).toEqual([
"enumconfwrite",
"enumconf",

View File

@ -43,7 +43,7 @@ const inst = {
};
expect(inst.hasOwnProperty("decoratedProps")).toBeTruthy();
expect(inst).toHaveProperty("decoratedProps");
expect(inst.decoratedProps).toEqual([
"enumconfwrite",
"enumconf",

View File

@ -42,7 +42,7 @@ const inst = {
_: 8,
};
expect(inst.hasOwnProperty("decoratedProps")).toBeTruthy();
expect(inst).toHaveProperty("decoratedProps");
expect(inst.decoratedProps).toEqual([
"enumconfwrite",
"enumconf",

View File

@ -12,8 +12,8 @@ expect(C.b()).toBe('B.b');
class D extends Object {}
expect(D instanceof Object).toBeTruthy();
expect(D.prototype instanceof Object).toBeTruthy();
expect(D).toBeInstanceOf(Object);
expect(D.prototype).toBeInstanceOf(Object);
expect(D.keys).toBe(Object.keys);
class E {}

View File

@ -5,5 +5,5 @@ class Foo {
}
const f = new Foo;
expect(f instanceof Foo).toBeTruthy();
expect(f).toBeInstanceOf(Foo);
expect(typeof f).toBe("object");

View File

@ -11,8 +11,8 @@ class List extends Array {
}
}
expect(new List(1) instanceof List).toBe(true);
expect(new List(2) instanceof Array).toBe(true);
expect(new List(1)).toBeInstanceOf(List);
expect(new List(2)).toBeInstanceOf(Array);
var l = new List(3);
expect(l).toHaveLength(1);
@ -27,9 +27,9 @@ class SecondLevel extends List {
}
}
expect(new SecondLevel(1) instanceof SecondLevel).toBe(true);
expect(new SecondLevel(2) instanceof List).toBe(true);
expect(new SecondLevel(3) instanceof Array).toBe(true);
expect(new SecondLevel(1)).toBeInstanceOf(SecondLevel);
expect(new SecondLevel(2)).toBeInstanceOf(List);
expect(new SecondLevel(3)).toBeInstanceOf(Array);
var s = new SecondLevel(4);
expect(s).toHaveLength(1);

View File

@ -1,4 +1,4 @@
class List extends Array {}
expect(new List instanceof List).toBe(true);
expect(new List instanceof Array).toBe(true);
expect(new List).toBeInstanceOf(List);
expect(new List).toBeInstanceOf(Array);

View File

@ -1,4 +1,4 @@
class List extends Array {}
expect(new List instanceof List).toBe(true);
expect(new List instanceof Array).toBe(true);
expect(new List).toBeInstanceOf(List);
expect(new List).toBeInstanceOf(Array);

View File

@ -4,7 +4,7 @@ foo[Symbol.hasInstance]= function () { return true; };
var bar = {};
expect(bar instanceof foo).toBe(true);
expect(new String instanceof String).toBe(true);
expect(new String).toBeInstanceOf(String);
//
@ -21,5 +21,5 @@ Object.defineProperty(Greeting, Symbol.hasInstance, {
var a = new Greeting("hello");
var b = new Greeting("world");
expect(a instanceof Greeting).toBe(true);
expect(b instanceof Greeting).toBe(false);
expect(a).toBeInstanceOf(Greeting);
expect(b instanceof Greeting).not.toBe(true);

View File

@ -1,22 +0,0 @@
var a = 1;
function rest() {
var b = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : a;
expect(b).toBe(1);
}
rest(undefined, 2);
function rest2() {
var b = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : a;
expect(arguments.length <= 1 ? undefined : arguments[1]).toBe(2);
}
rest2(undefined, 2);
function rest3() {
var b = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : a;
expect(arguments.length <= 1 ? 0 : arguments.length - 1).toBe(1);
}
rest3(undefined, 2);

View File

@ -16,4 +16,4 @@ tally.next(0.1);
tally.next(0.1);
tally.next(0.1);
let last = tally.next("done");
expect(last.value).toBe(0.30000000000000004);
expect(last.value).toBeCloseTo(0.3, 10);

View File

@ -2,4 +2,4 @@ class Foo {
}
expect(new Foo().constructor).toBe(Foo);
expect(new Foo() instanceof Foo).toBeTruthy();
expect(new Foo()).toBeInstanceOf(Foo);

View File

@ -108,7 +108,7 @@ class MyArray extends Array {
}
var ma = MyArray.from(['a', 'b']);
expect(ma instanceof MyArray).toBe(true);
expect(ma).toBeInstanceOf(MyArray);
expect(constructorCounter).toBe(1);
expect(lengthSetCounter).toBe(1);
expect(lengthGetCounter).toBe(0);
@ -146,7 +146,7 @@ var ma3 = new MyArray3(2);
ma3[0] = 'a';
ma3[1] = 'b';
ma = MyArray2.from(ma3);
expect(ma instanceof MyArray2).toBe(true);
expect(ma).toBeInstanceOf(MyArray2);
expect(constructorCounter).toBe(2);
expect(lengthSetCounter).toBe(1);
expect(lengthGetCounter).toBe(0);

View File

@ -6,12 +6,12 @@ expect(Array.of.length).toBe(0);
//should return an array from arguments
arr = Array.of(1, 'a', 3);
expect(arr).toEqual([1, 'a', 3]);
// expect(arr instanceof Array).toBe(true);
// expect(arr).toBeInstanceof(Array);
//should work with no arguments
arr = Array.of();
expect(arr).toEqual([]);
// expect(arr instanceof Array).toBe(true);
// expect(arr).toBeInstanceof(Array);
//should work with sub-classed array
class MyArray extends Array {}
@ -20,7 +20,7 @@ arr = MyArray.of(4, 'b');
expect(arr[0]).toBe(4);
expect(arr[1]).toBe('b');
expect(arr).toHaveLength(2);
// expect(arr instanceof MyArray).toBe(true);
// expect(arr).toBeInstanceof(MyArray);
//should call with exotic array
class ExoticArray {
@ -34,4 +34,4 @@ expect(arr[1]).toBe('c');
expect(arr[2]).toBe(6);
expect(arr[3]).toBe('d');
expect(arr).toHaveLength(4);
expect(arr instanceof ExoticArray).toBe(true);
expect(arr).toBeInstanceOf(ExoticArray);

View File

@ -4,4 +4,4 @@ async function f() {
}
expect(Object.getPrototypeOf(f)).toBe(Function.prototype);
expect(f() instanceof Promise).toBe(true);
expect(f()).toBeInstanceOf(Promise);

View File

@ -20,4 +20,4 @@ class E {}
expect(Object.getPrototypeOf(E)).toBe(Function.prototype);
expect(Object.getPrototypeOf(E.prototype)).toBe(Object.prototype);
expect('keys' in E).toBe(false);
expect(E).not.toHaveProperty('keys');

View File

@ -12,7 +12,7 @@ class DerivedConstructorMember extends ConstructorMember {
var cm = new ConstructorMember;
expect(cm.constructor).toBe(ConstructorMember.prototype.constructor);
expect(ConstructorMember.prototype.hasOwnProperty('constructor')).toBe(true);
expect(ConstructorMember.prototype).toHaveProperty('constructor');
for (var key in ConstructorMember) {
expect('constructor should not be enumerable').not.toBe('constructor');
@ -20,7 +20,7 @@ for (var key in ConstructorMember) {
var dcm = new DerivedConstructorMember;
expect(dcm.constructor).toBe(DerivedConstructorMember.prototype.constructor);
expect(DerivedConstructorMember.prototype.hasOwnProperty('constructor')).toBe(true);
expect(DerivedConstructorMember.prototype).toHaveProperty('constructor');
for (var key in DerivedConstructorMember) {
expect('constructor should not be enumerable').not.toBe('constructor');

View File

@ -20,10 +20,10 @@ for (var element in Empty) {
var e2 = new Empty();
expect(e).not.toBe(e2);
expect(e instanceof Empty).toBe(true);
expect(e instanceof EmptyB).toBe(false);
expect(e).toBeInstanceOf(Empty);
expect(e).not.toBeInstanceOf(EmptyB);
var b = new EmptyB();
expect(b instanceof Empty).toBe(true);
expect(b instanceof EmptyB).toBe(true);
expect(b).toBeInstanceOf(Empty);
expect(b).toBeInstanceOf(EmptyB);

View File

@ -6,5 +6,5 @@ class D extends (x, y) {
}
expect(new D() instanceof C).toBe(true);
expect(new D() instanceof D).toBe(true);
expect(new D()).toBeInstanceOf(C);
expect(new D()).toBeInstanceOf(D);

View File

@ -1,8 +1,8 @@
class C extends null {}
var c = new C;
expect(c instanceof C).toBe(true);
expect(c instanceof Object).toBe(false);
expect(c).toBeInstanceOf(C);
expect(c).toBeInstanceOf(Object);
// Closure testing framework tries to toString the object and fails.
expect(Object.getPrototypeOf(c)).toBe(C.prototype);

View File

@ -4,6 +4,6 @@ class SimpleDerived extends SimpleBase {}
// ----------------------------------------------------------------------------
var derived = new SimpleDerived();
expect(derived instanceof SimpleDerived).toBe(true);
expect(derived instanceof SimpleBase).toBe(true);
expect(derived instanceof Object).toBe(true);
expect(derived).toBeInstanceOf(SimpleDerived);
expect(derived).toBeInstanceOf(SimpleBase);
expect(derived).toBeInstanceOf(Object);

View File

@ -25,7 +25,9 @@ var a = new MemberExprBase('w value');
var pa = Object.getPrototypeOf(a);
var ppa = Object.getPrototypeOf(pa);
assertHasOwnProperty(a, 'yyy', 'w', 'z');
assertLacksOwnProperty(a, 'x');
assertHasOwnProperty(pa, 'constructor');
assertHasOwnProperty(ppa, 'x', 'constructor');
expect(a).toHaveProperty('yyy');
expect(a).toHaveProperty('w');
expect(a).toHaveProperty('z');
expect(a).not.toHaveProperty('x');
expect(pa).toHaveProperty('constructor');
expect(ppa).toHaveProperty('constructor');

View File

@ -12,6 +12,6 @@ class NoClassB extends noClassA {
// ----------------------------------------------------------------------------
var b = new NoClassB;
expect(b instanceof noClassA).toBe(true);
expect(b).toBeInstanceOf(noClassA);
expect('ma').toBe(b.ma());
expect('mb ma').toBe(b.mb());

View File

@ -24,9 +24,12 @@ assertNoOwnProperties(a);
assertNoOwnProperties(b);
assertNoOwnProperties(c);
assertHasOwnProperty(pa, 'ma');
assertLacksOwnProperty(pa, 'mb', 'mc');
assertHasOwnProperty(pb, 'mb');
assertLacksOwnProperty(pb, 'ma', 'mc');
assertHasOwnProperty(pc, 'mc');
assertLacksOwnProperty(pc, 'ma', 'mb');
expect(pa).toHaveProperty('ma');
expect(pa).not.toHaveProperty('mb');
expect(pa).not.toHaveProperty('mc');
expect(pb).toHaveProperty('mb');
expect(pb).not.toHaveProperty('ma');
expect(pb).not.toHaveProperty('mc');
expect(pc).toHaveProperty('mc');
expect(pc).not.toHaveProperty('ma');
expect(pc).not.toHaveProperty('mb');

View File

@ -35,6 +35,6 @@ try {
}
expect(actualF).toBe(expectedF);
expect(expectedM instanceof TypeError).toBe(true);
expect(actualM instanceof TypeError).toBe(true);
expect(expectedM).toBeInstanceOf(TypeError);
expect(actualM).toBeInstanceOf(TypeError);
expect(Object.getPrototypeOf(actualM)).toBe(Object.getPrototypeOf(expectedM));

View File

@ -4,7 +4,7 @@ var assigned = Object.assign(target, source);
// ----------------------------------------------------------------------------
expect(Object.hasOwnProperty("assign")).toBe(true);
expect(Object).toHaveProperty("assign");
expect(assigned.a).toBe(source.a);
expect(assigned.b).toBe(target.b);
expect(assigned.c).toBe(source.c);

View File

@ -9,7 +9,7 @@ throwInResolve.then((v) => {
}).catch(function(ex) {
// When catch() is used in testing, these asserts would
// not be called, just the done() to avoid timeout.
expect(ex instanceof Error).toBe(true);
expect(ex).toBeInstanceOf(Error);
expect(ex.toString()).toEqual(expect.stringContaining(message));
done();
}).catch(done);

View File

@ -4,4 +4,4 @@ export default class C {
}
}
expect(C instanceof Function).toBe(true);
expect(C).toBeInstanceOf(Function);

View File

@ -2,4 +2,4 @@ export default function f() {
return 123;
}
expect(f instanceof Function).toBe(true);
expect(f).toBeInstanceOf(Function);

View File

@ -84,18 +84,17 @@
static set 0O13(v) {}
}
expect(C.prototype.hasOwnProperty('0')).toBe(true);
expect(C.prototype.hasOwnProperty('1')).toBe(true);
expect(C.prototype.hasOwnProperty('2')).toBe(true);
expect(C.prototype.hasOwnProperty('6')).toBe(true);
expect(C.prototype.hasOwnProperty('7')).toBe(true);
expect(C.prototype.hasOwnProperty('8')).toBe(true);
expect(C.hasOwnProperty('3')).toBe(true);
expect(C.hasOwnProperty('4')).toBe(true);
expect(C.hasOwnProperty('5')).toBe(true);
expect(C.hasOwnProperty('9')).toBe(true);
expect(C.hasOwnProperty('10')).toBe(true);
expect(C.hasOwnProperty('11')).toBe(true);
expect(C.prototype).toHaveProperty('0');
expect(C.prototype).toHaveProperty('1');
expect(C.prototype).toHaveProperty('2');
expect(C.prototype).toHaveProperty('6');
expect(C.prototype).toHaveProperty('7');
expect(C.prototype).toHaveProperty('8');
expect(C).toHaveProperty('3');
expect(C).toHaveProperty('4');
expect(C).toHaveProperty('5');
expect(C).toHaveProperty('9');
expect(C).toHaveProperty('10');
expect(C).toHaveProperty('11');
})();

View File

@ -35,7 +35,7 @@ expect(Object.keys(object)).toEqual([
]);
function assertMethod(object, name) {
expect(object.hasOwnProperty(name)).toBe(true);
expect(object).toHaveProperty(name);
var descriptor = Object.getOwnPropertyDescriptor(object, name);
expect(typeof descriptor).toBe('object');
expect(descriptor.enumerable).toBe(true);

View File

@ -10,6 +10,6 @@ for (let x in obj) {
// ----------------------------------------------------------------------------
expect(result[0]()).toBe('hello a');;
expect(result[1]()).toBe('hello b');;
expect(result[2]()).toBe('hello c');;
expect(result[0]()).toBe('hello a');
expect(result[1]()).toBe('hello b');
expect(result[2]()).toBe('hello c');

View File

@ -14,4 +14,4 @@ var object = new SpreadTestClass(...[0, 1]);
expect(object.x).toBe(0);
expect(object.y).toBe(1);
expect(object.self).toBe(object);
expect(object instanceof object.SpreadTestClass).toBe(true);
expect(object).toBeInstanceOf(object.SpreadTestClass);

View File

@ -18,6 +18,6 @@ var object = new F(0, ...[1, 2]);
expect(object.x).toBe(0);
expect(object.y).toBe(1);
expect(object.z).toBe(2);
expect(object instanceof object.F).toBe(false);
expect(object instanceof object.G).toBe(true);
expect(object.f instanceof object.F).toBe(true);
expect(object).not.toBeInstanceOf(object.F);
expect(object).toBeInstanceOf(object.G);
expect(object.f).toBeInstanceOf(object.F);

View File

@ -5,7 +5,7 @@ expect(object[s]).toBe(42);
// Native Symbol throws for ToString.
// expect(object[s + '']).toBeUndefined();
expect(Object.getOwnPropertyNames(object)).toEqual([]);
expect(object.hasOwnProperty(s)).toBe(true);
expect(object).toHaveProperty(s);
expect(object[s] -= 10).toBe(32);
expect(object[s] /= 2).toBe(16);

View File

@ -1,7 +1,7 @@
var s = Symbol('s');
expect(typeof s).toBe('symbol');
expect(s.constructor).toBe(Symbol);
expect(s instanceof Symbol).toBe(false);
expect(s).toBeInstanceOf(Symbol);
expect(() => {
new Symbol;

View File

@ -10,7 +10,7 @@ expect(GeneratorFunctionPrototype.prototype).toBe(f.prototype.__proto__);
expect(GeneratorFunctionPrototype.__proto__).toBe(Function.prototype);
var g = f();
expect(g instanceof f).toBe(true);
expect(g).toBeInstanceOf(f);
expect(g.__proto__).toBe(f.prototype);
@ -31,7 +31,7 @@ function* f2() {
expect(f.__proto__).toBe(f2.__proto__);
expect(f.hasOwnProperty('constructor')).toBe(false);
expect(f).not.toHaveProperty('constructor');
expect(f.__proto__.constructor.name).toBe('GeneratorFunction');
// Intentionally at the end to test hoisting.

View File

@ -11,4 +11,4 @@ var g = f2();
expect(g.next()).toEqual({value: 1, done: false});
expect(g.next()).toEqual({value: undefined, done: true});
expect(g instanceof f2).toBe(true);
expect(g).toBeInstanceOf(f2);