stalniy/bdd-lazy-var

Ability to deep merge with parent def

Closed this issue · 6 comments

This is something that I find myself doing a lot. Defining an object in a parent context and then merging in changes to be more specific in sub describes. It would be very handy to have something like mergeDef which basically just deep merges that def with the previous contexts def

Love the library, by the way, been using it for years and couldn't go back.

def("foo", () => {foo: { bar: 1, baz: 2 } });

describe("when foo.bar is 2", () => {
  mergeDef("foo", () => {foo: {bar: 2}});

  it("sets both foo.bar and foo.baz to 2", () => {
    expect($foo).toEqual({foo: {foo: 2, baz: 2}});
  }); 
});

Interesting. I think it’s possible to add to def modifiers in such a way def.merge(“foo”, () => ({ bar: 1 }))

Actually I think you can write your own:

def.merge = (name, fn) => def(name, () => _.merge({}, get(name), fn())

Better to reuse def so then you don’t need to modify eslint config and it looks more consistent to me

That sounds even better, didn't think about extending def.

Not sure where I would put that snippet? Make a wrapper around def and import that instead?

What framework do you use?

Jest

You can use setupFilesAfterEnv

Close as it can be implemented in userland