CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-hapi--code

BDD assertion library designed to work seamlessly with the hapi ecosystem

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

string-assertions.mddocs/

String-Specific Assertions

Specialized string operations for prefix and suffix checking.

Capabilities

startWith() Method

Asserts that the reference string starts with the provided value. Case-sensitive comparison.

/**
 * Asserts that the reference string starts with provided value
 * @param {string} value - String value to check for at start
 * @returns {Assertion} Assertion chain object for further chaining
 */
startWith(value)

Alias: startsWith()

Usage Examples:

const Code = require('@hapi/code');
const expect = Code.expect;

// Basic usage
expect('hello world').to.startWith('hello');
expect('JavaScript').to.startWith('Java');
expect('https://example.com').to.startWith('https://');

// Case sensitivity
expect('Hello').to.startWith('Hello'); // Passes
expect('Hello').to.not.startWith('hello'); // Passes - case sensitive

// Empty string
expect('anything').to.startWith(''); // Passes - all strings start with empty string

// Whole string match
expect('hello').to.startWith('hello'); // Passes - can match entire string

// Common patterns
const url = getApiUrl();
expect(url).to.be.a.string().and.startWith('https://');

const filename = getFilename();
expect(filename).to.startWith('temp_').and.endWith('.txt');

endWith() Method

Asserts that the reference string ends with the provided value. Case-sensitive comparison.

/**
 * Asserts that the reference string ends with provided value
 * @param {string} value - String value to check for at end
 * @returns {Assertion} Assertion chain object for further chaining
 */
endWith(value)

Alias: endsWith()

Usage Examples:

// Basic usage
expect('hello world').to.endWith('world');
expect('filename.txt').to.endWith('.txt');
expect('path/to/file.js').to.endWith('.js');

// Case sensitivity
expect('Hello').to.endWith('Hello'); // Passes
expect('Hello').to.not.endWith('hello'); // Passes - case sensitive

// Empty string
expect('anything').to.endWith(''); // Passes - all strings end with empty string

// Whole string match
expect('hello').to.endWith('hello'); // Passes - can match entire string

// File extensions
const files = ['image.jpg', 'doc.pdf', 'script.js'];
files.forEach(file => {
    if (file.endsWith('.js')) {
        expect(file).to.endWith('.js');
    }
});

Advanced Usage Patterns

Combining String Assertions

const Code = require('@hapi/code');
const expect = Code.expect;

// Chain multiple string checks
expect('hello world')
    .to.be.a.string()
    .and.startWith('hello')
    .and.endWith('world')
    .and.include('o w')
    .and.have.length(11);

// URL validation pattern
const apiUrl = 'https://api.example.com/v1/users';
expect(apiUrl)
    .to.be.a.string()
    .and.startWith('https://')
    .and.include('api.')
    .and.endWith('/users');

// File path validation
const tempFile = '/tmp/upload_12345.tmp';
expect(tempFile)
    .to.startWith('/tmp/')
    .and.endWith('.tmp')
    .and.include('upload_');

Pattern Matching with Regular Expressions

While startWith() and endWith() are for exact string matching, you can combine them with regex matching:

// Exact prefix/suffix with pattern matching in middle
const logEntry = '[2023-12-01] INFO: User logged in';
expect(logEntry)
    .to.startWith('[')
    .and.endWith('logged in')
    .and.match(/\[\d{4}-\d{2}-\d{2}\]/);

// Validate email-like strings
const email = 'user@example.com';
expect(email)
    .to.include('@')
    .and.endWith('.com')
    .and.match(/^[^@]+@[^@]+\.[^@]+$/);

Error Conditions

// These will throw errors - only works with strings
try {
    expect(123).to.startWith('1'); // Error: Can only assert startsWith on string
} catch (err) {
    expect(err).to.be.an.error();
}

try {
    expect(['hello']).to.endWith('lo'); // Error: Can only assert endsWith on string
} catch (err) {
    expect(err).to.be.an.error();
}

// Type checking before string assertions
const value = getValue();
if (typeof value === 'string') {
    expect(value).to.startWith('prefix');
} else {
    expect(value).to.not.be.a.string();
}

Common Use Cases

// API response validation
const response = await fetch('/api/data');
const location = response.headers.get('location');
if (location) {
    expect(location).to.startWith('/api/');
}

// Configuration validation
const config = {
    database_url: 'postgresql://localhost:5432/mydb',
    redis_url: 'redis://localhost:6379',
    log_file: '/var/log/app.log'
};

expect(config.database_url).to.startWith('postgresql://');
expect(config.redis_url).to.startWith('redis://');
expect(config.log_file).to.startWith('/var/log/').and.endWith('.log');

// Template string validation
const template = '{{user.name}} has {{user.points}} points';
expect(template).to.startWith('{{').and.endWith('}}');
expect(template).to.include('{{user.name}}').and.include('{{user.points}}');

docs

assertion-modifiers.md

content-testing.md

core-functions.md

function-testing.md

index.md

numeric-comparisons.md

pattern-validation.md

promise-testing.md

settings.md

string-assertions.md

type-assertions.md

value-assertions.md

tile.json