How do you unit test a shared library?
AdvancedConceptShared libraries
Answer
Use JenkinsPipelineUnit (Groovy, run with Gradle or Maven). It loads library scripts, mocks steps (helper.registerAllowedMethod('sh', [Map]) { ... }) and records the call stack so you can assert what commands would run.
class TrivyScanTest extends BasePipelineTest {
@Test void failsOnHighByDefault() {
def script = loadScript('vars/trivyScan.groovy')
script.call(input: 'image.tar')
assert helper.callStack.find { it.methodName == 'sh' }.argsToString().contains('--severity HIGH,CRITICAL')
}
}
Integration test new versions with a test Jenkinsfile on a staging controller before moving the v1 tag.
More on Shared libraries
What is the directory structure of a shared library?BeginnerHow does a `vars/` global step work?BeginnerWhat goes in `src/` and how is it used?IntermediateHow do classes in `src/` call pipeline steps?IntermediateWhat is `libraryResource` used for?IntermediateWhat are the ways to load a library in a Jenkinsfile?Intermediate