How to use the exist function from should

Find comprehensive JavaScript should.exist code examples handpicked from public code repositorys.

should.exist is a method provided by the should.js assertion library that checks whether a given value is not null or undefined.

30
31
32
33
34
35
36
37
38
});

it('test prepareZone error', function (done) {
    config.zone = null;
    qiniu.util.prepareZone(bucketManager, 'no_ak', 'no_bucket', function (err, ctx) {
        should.exist(err);
        done();
    });
});
fork icon189
star icon577
watch icon38

36
37
38
39
40
41
42
43
44
45

it('report an error for circular dependency objects', () => {
  const firstLevel = { a: 1 }
  const secondLevel = { b: 2, c: firstLevel }
  firstLevel.second = secondLevel
  should.exist(sizeof(firstLevel))
})

it('handle hasOwnProperty key', () => {
  sizeof({ hasOwnProperty: undefined }).should.be.instanceOf(Number)
fork icon39
star icon282
watch icon3

+ 17 other calls in file

How does should.exist work?

should.exist is a method provided by the should.js assertion library that checks whether a given value is not null or undefined. The method is typically used as part of a unit test to verify that a variable or function returns an expected value. Under the hood, should.exist checks whether the value passed to it is not null or undefined. If the value is null or undefined, an assertion error is thrown. If the value is not null or undefined, the method returns undefined, indicating that the assertion passed. For example, the following code uses should.exist to test whether a variable named myVariable is not null or undefined: javascript Copy code {{{{{{{ const should = require('should'); // Test whether a variable exists const myVariable = 'hello'; should.exist(myVariable); If myVariable is null or undefined, an assertion error will be thrown, indicating that the test has failed. Otherwise, the test will pass and the method will return undefined. Overall, should.exist provides a simple and convenient way to test whether a value is not null or undefined, helping to ensure that your code behaves correctly under various conditions.

100
101
102
103
104
105
106
107
108
109
'internal3',
'smartgondor',
'gardens',
function (error, devices) {
    should.not.exist(error);
    should.exist(devices);
    devices.length.should.equal(1);
    devices[0].id.should.equal('id3');
    done();
}
fork icon74
star icon56
watch icon23

+ 5 other calls in file

27
28
29
30
31
32
33
34
35
36
const { count } = await container.one(sql`select count(*) from entities`);
count.should.equal(0);

// Original submission update event should look like it was successfully processed with no failures.
const updateEvent = await container.Audits.getLatestByAction('submission.update').then((o) => o.get());
should.exist(updateEvent.processed);
updateEvent.failures.should.equal(0);

// There should be no entity events logged.
const createEvent = await container.Audits.getLatestByAction('entity.create');
fork icon49
star icon44
watch icon9

+ 27 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
const should = require("should");

// Define a function that returns a value
function multiply(a, b) {
  return a * b;
}

// Test whether the function returns a value
const result = multiply(2, 3);
should.exist(result);

In this example, should.exist is used to test whether the multiply function returns a value. The function takes two arguments and returns their product. The result variable is assigned the result of calling multiply with arguments 2 and 3. Then, should.exist is used to test whether result is not null or undefined. If multiply returns a value, the test will pass and no error will be thrown. If the function returns null or undefined, the test will fail and an assertion error will be thrown. Note that should.exist can be used in various contexts to test whether a value is not null or undefined, helping to ensure that your code behaves correctly and returns expected values.

261
262
263
264
265
266
267
268
269
270
  return done(articleSaveErr);
}

// Set assertions on new article
(articleSaveRes.body.title).should.equal(article.title);
should.exist(articleSaveRes.body.user);
should.equal(articleSaveRes.body.user._id, orphanId);

// force the article to have an orphaned user reference
orphan.remove(function () {
fork icon0
star icon1
watch icon1

+ 7 other calls in file

45
46
47
48
49
50
51
52
53
54
    keyCount.should.equal(Object.keys(defaultValues).length);
});

it('reportstorage.saveProps sets property in localstorage', () => {
    reportstorage.saveProps({insulin: false});
    should.exist(mockStorage.get('reportProperties'));
    mockStorage.get('reportProperties').insulin.should.be.false();
});

it('reportstorage.saveProps ignores property not tracked', () => {
fork icon0
star icon1
watch icon1

+ 5 other calls in file

36
37
38
39
40
41
42
43
44
45
    return done(err);
}

should.not.exist(res.headers['x-cache-invalidate']);
const jsonResponse = res.body;
should.exist(jsonResponse.posts);
localUtils.API.checkResponse(jsonResponse, 'posts');
jsonResponse.posts.should.have.length(13);

localUtils.API.checkResponse(
fork icon0
star icon1
watch icon0

+ 68 other calls in file

403
404
405
406
407
408
409
410
411
    // perform a sign-in request to set members cookies on superagent
    await request.get(signinPath)
        .expect(302)
        .then((res) => {
            const redirectUrl = new URL(res.headers.location, testUtils.API.getURL());
            should.exist(redirectUrl.searchParams.get('success'));
            redirectUrl.searchParams.get('success').should.eql('true');
        });
});
fork icon0
star icon1
watch icon0

+ 3 other calls in file

89
90
91
92
93
94
95
96
97
98
    'users',
    'webhooks',
    'milestones'
];

should.exist(exportData);
should.exist(exportData.meta);
should.exist(exportData.data);

// NOTE: using `Object.keys` here instead of `should.have.only.keys` assertion
fork icon0
star icon0
watch icon1

+ 7 other calls in file

39
40
41
42
43
44
45
46
47
48
                    name: 'SiteRouter'
                }]
            }
        };

        should.exist(parentRouter._getSiteRouter(req));
    });
});

describe('fn: _respectDominantRouter', function () {
fork icon0
star icon0
watch icon1

+ 5 other calls in file

26
27
28
29
30
31
32
33
34
    await configUtils.restore();
});

it('should output relative url of image', function () {
    const rendered = img_url('/content/images/image-relative-url.png', {});
    should.exist(rendered);
    rendered.should.equal('/content/images/image-relative-url.png');
    logWarnStub.called.should.be.false();
});
fork icon0
star icon0
watch icon1

+ 53 other calls in file

753
754
755
756
757
758
759
760
761
762
const model = await models.Post.findOne({
    id
}, testUtils.context.internal);

should(model.get('newsletter_id')).eql(null);
should.exist(model.get('published_by'));

// Check email
// Note: we only create an email if we have members susbcribed to the newsletter
const email = await models.Email.findOne({
fork icon0
star icon0
watch icon1

+ 47 other calls in file

44
45
46
47
48
49
50
51
52
53
  const result = await furkotDirections({
    services: []
  })({
    points: [[0, 0], [1, 1]]
  });
  should.exist(result);
  result.should.have.property('routes').with.length(1);
});

it('service', async function () {
fork icon0
star icon0
watch icon3

8
9
10
11
12
13
14
15
16
17
18
describe('channel manager test', function () {
    describe('#createChannel', function () {
        it('should create and return a channel with the specified name', function () {
            let channelService = new ChannelService(mockApp);
            let channel = channelService.createChannel(channelName);
            should.exist(channel);
            channelName.should.equal(channel.name);
        });


        it('should return the same channel if the name has already existed', function () {
fork icon0
star icon0
watch icon2

+ 26 other calls in file

197
198
199
200
201
202
203
204
205
206
//         for (i = 0, l = filters2.length; i < l; i++) {
//             filters2[i].should.equal(filters[i]);
//         }
//
//         let filters3 = app.get('__afters__');
//         should.exist(filters3);
//         filters3.length.should.equal(filters.length);
//         for (i = 0, l = filters3.length; i < l; i++) {
//             filters2[i].should.equal(filters[i]);
//         }
fork icon0
star icon0
watch icon2

+ 44 other calls in file

2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
    });

res.text.should.match(/id,email,name,note,subscribed_to_emails,complimentary_plan,stripe_customer_id,created_at,deleted_at,labels,tiers/);

const csv = Papa.parse(res.text, {header: true});
should.exist(csv.data.find(row => row.name === 'Mr Egg'));
should.exist(csv.data.find(row => row.name === 'Winston Zeddemore'));
should.exist(csv.data.find(row => row.name === 'Ray Stantz'));
should.exist(csv.data.find(row => row.email === 'member2@test.com'));
should.exist(csv.data.find(row => row.tiers.length > 0));
fork icon0
star icon0
watch icon0

+ 7 other calls in file

14
15
16
17
18
19
20
21
22
23
24
25
    // storing current environment
    currentEnv = process.env.NODE_ENV;


describe('Error handling', function () {
    // Just getting rid of jslint unused error
    should.exist(errors);


    describe('Throwing', function () {
        it('throws error objects', function () {
            var toThrow = new Error('test1'),
fork icon0
star icon0
watch icon2

29
30
31
32
33
34
35
36
37
38
});

it('createCheckoutSession sends success_url and cancel_url', async function (){
    await api.createCheckoutSession('priceId', null, {});

    should.exist(mockStripe.checkout.sessions.create.firstCall.firstArg.success_url);
    should.exist(mockStripe.checkout.sessions.create.firstCall.firstArg.cancel_url);
});

it('createCheckoutSetupSession sends success_url and cancel_url', async function (){
fork icon0
star icon0
watch icon1

+ 23 other calls in file

13
14
15
16
17
18
19
20
21
22
        expires: expires,
        password: 'password',
        dbHash: dbHash
    });

    should.exist(token);
    token.length.should.be.above(0);
});

it('compare: success', function () {
fork icon0
star icon0
watch icon1

187
188
189
190
191
192
193
194
195
196
executeMiddleware(middleware, req, res, function next() {
    try {
        const templateOptions = hbs.updateTemplateOptions.firstCall.args[0];
        const data = templateOptions.data;

        should.exist(data.site.signup_url);
        data.site.signup_url.should.equal('https://feedly.com/i/subscription/feed/http%3A%2F%2F127.0.0.1%3A2369%2Frss%2F');

        done();
    } catch (error) {
fork icon0
star icon0
watch icon1

+ 3 other calls in file